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)
}
}