swift创建一个空项目

前端之家收集整理的这篇文章主要介绍了swift创建一个空项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class AppDelegate: UIResponder,UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //创建根控制器
        window?.rootViewController = ViewController()
        //显示window
        window?.makeKeyAndVisible()
        
        return true
    }

}





在Controller中监听按钮点击:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        view.backgroundColor = UIColor.greenColor()
        
        //在控制器中添加一个按钮
        let btn = UIButton(type: UIButtonType.ContactAdd)
        
        btn.center = CGPoint(x: UIScreen.mainScreen().bounds.width * 0.5,y: UIScreen.mainScreen().bounds.height * 0.5)
        //给按钮添加事件
        btn.addTarget(self,action: "btnClick:",forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn)
    }

    func btnClick(btn: UIButton) {
        print(__FUNCTION__)
        print(btn)
    }
}

猜你在找的Swift相关文章