在 swift 3 按钮单击应用程序崩溃,它与故事板绑定

in swift 3 on button click application is crash which bind with story board

我在 Swift 2.2 中开发了一个项目,在 Swift 3 启动后,我需要在 Swift 3 中转换它并使其可与 iOS 10 编译。但是转换项目后,我遇到了一些问题,例如当我单击与情节提要相连的按钮时,我的应用程序崩溃并给出原因 unrecognized selector sent to instance 。它在 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)
              }
         }

只需从故事板的连接检查器中删除所有 Outlets 和 IBActions,然后重新添加它们。之后一切都应该正常工作

如果您以编程方式向按钮添加操作,您的选择器语法类似于 #selector(btnTwitter_Clicked(_:)) 这不适用于 Swift 3 从 Swift 3 开始,您需要指定第一个参数名称,所以要么在你的方法中添加 _ 作为第一个参数名称,如 func btnTwitter_Clicked(_ sender: UIButton),要么像这样更改你的选择器语法,其中一个将解决你的崩溃问题。

 #selector(btnTwitter_Clicked(sender:)) 

看到这个

@@IBAction func btnTwitter_Clicked(sender: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重新生成一次,肯定可以