我已经在Swift 2.2中开发了一个项目,在Swift 3发布之后我需要在Swift 3中进行转换并与iOS 10进行编译。但是在转换项目之后我遇到了一些问题,比如当我点击与故事板连接的按钮时我的应用程序崩溃,并将无法识别的选择器发送到实例。它在swift 2.2中工作,我没有改变任何东西。
@IBAction func btnTwitter_Clicked(sender:UIButton)
{
if checkInternetConnection()
{
SINGLETON.startLoadingActivity(self.view);
let objLocationTracker = LocationTracker.sharedInstance
objLocationTracker.fetchCurrentLocation({ (objLocation) -> (Void) in
SINGLETON.stopLoadingActivity(self.view)
self.loginWithTwitter(objLocation: objLocation)
})
}
else
{
SINGLETON.toast(read_Localizable("noInternet"),view: self.view)
}
}
答案 0 :(得分:3)
看到这个
@@ IBAction func btnTwitter_Clicked(发件人:UIButton)您已在按钮前添加了 @ ,请检查一次,
在swift3中我们需要编写像这样的按钮动作yourbuttonName.addTarget(self, action: #selector(yourVCName. btnTwitter_Clicked(_:)), for: .touchUpInside)
并调用方法
@IBAction func btnTwitter_Clicked(_ sender: UIButton){
print("Button pressed ")
// continue your work
}
或删除按钮Outlets和IBActions并再次重新生成一次,肯定有效
答案 1 :(得分:1)
只需从故事板中的连接检查器中删除所有Outlets和IBActions,然后重新添加。之后一切都应该正常工作
答案 2 :(得分:1)
如果要以编程方式向按钮添加操作,则选择器语法类似于#selector(btnTwitter_Clicked(_:))
,这对于Swift 3的Swift 3不起作用,您需要指定第一个参数名称,以便添加_作为第一个参数名称在您的方法func btnTwitter_Clicked(_ sender: UIButton)
中或更改您的选择器语法,这样就可以解决您的崩溃问题。
#selector(btnTwitter_Clicked(sender:))