首页 > 解决方案 > 以编程方式“迅速”执行 segue

问题描述

我以编程方式在按钮中添加了“addTarget”属性以转到另一个视图控制器,但它不起作用。

login.addTarget(self, action: Selector(("toHome")), for: UIControl.Event.touchUpInside)

@objc func toHome(_ sender : UIButton!){
    self.performSegue(withIdentifier: "Index", sender: self)
}

标签: xcode

解决方案


Use the native selector syntax

login.addTarget(self, action: #selector(toHome), for: .touchUpInside)

and an implicit unwrapped optional as parameter is Swift 2 legacy syntax. Remove the exclamation mark

@objc func toHome(_ sender : UIButton) {
    self.performSegue(withIdentifier: "Index", sender: self)
}

推荐阅读