swift初见

SwiftUI视图与交互示例
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor .yellowColor();
        self.addView();
        self.addButton();
        self.addButton1();
        self.createLabel()
    }
    
    func createLabel() {
        let label = UILabel();
        label.text = "1234"
        label.frame = CGRectMake(100, 80, 100, 50);
        label.textAlignment = NSTextAlignment.Center;
        label.textColor = UIColor.blackColor()
        self.view.addSubview(label);
    }

    func addButton() {
        let clickButton = UIButton();
        clickButton.frame = CGRectMake(100, 100, 100, 100);
        clickButton.backgroundColor = UIColor.redColor();
        clickButton .setTitle("clock", forState: UIControlState.Normal);
        clickButton.addTarget(self, action: Selector("click:"), forControlEvents: UIControlEvents.TouchUpInside);
        self.view .addSubview(clickButton);
    }
    
    func addButton1() {
        let addButton1 = UIButton()
        addButton1.frame = CGRectMake(20, 200, 50, 60)
        addButton1.backgroundColor = UIColor.brownColor();
        addButton1.addTarget(self, action: Selector("tap:"), forControlEvents: UIControlEvents.TouchUpInside)
        addButton1.tag = 101;
        self.view .addSubview(addButton1);
    }
    
    func click(sender:UIButton) {
        let alertController = UIAlertController(title: "alert", message: "my is young", preferredStyle: UIAlertControllerStyle.Alert)
        let alertAction = UIAlertAction(title: ">>>>>", style: UIAlertActionStyle.Destructive, handler: nil)
        let alertAction1 = UIAlertAction(title: "click", style: UIAlertActionStyle.Cancel, handler: nil)
        let alertAction2 = UIAlertAction(title: "[uhk", style: UIAlertActionStyle.Default, handler: nil)
        
        alertController.addAction(alertAction2)
        alertController.addAction(alertAction1)
        alertController.addAction(alertAction)
       
        self.presentViewController(alertController, animated: true, completion: nil)
        print(sender.tag)
    }
    
    func tap(sender:UIButton) {
        let alertController = UIAlertController(title: "second alert", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
//        let alertAction = UIAlertAction();
        alertController.addTextFieldWithConfigurationHandler { (UITextField) -> Void in
            print(sender.tag)
        }
        let alertAction = UIAlertAction(title: "yes", style: UIAlertActionStyle.Default, handler: nil)
        let alertAction2 = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: nil)
        alertController.addAction(alertAction)
        alertController.addAction(alertAction2)
        self.presentViewController(alertController, animated: true, completion: nil)
        
    }
    
    func addView() {
        let view = UIView();
        view.frame = CGRectMake(100, 100, 200, 300);
        view.backgroundColor = UIColor.blackColor();
        self.view.addSubview(view);
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true);
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值