swift初见

来源:互联网 发布:matlab中蚁群算法 编辑:程序博客网 时间:2024/05/31 13:14
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.
    }


}

0 0