1.删除StoryBoard,需要设置Main为空以及删除plist文件中对应StoryBoard一栏,否则crash
2.按钮添加点击方法正确姿势
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.red
let btn = UIButton(type: UIButton.ButtonType.contactAdd)
btn.center = CGPoint(x: 100, y: 100)
btn.addTarget(self, action:#selector(btnClick), for: UIControl.Event.touchUpInside)
view.addSubview(btn)
}
@objc func btnClick() {
print("点击了按钮")
}
}