ios - 在 swift 3 中,按钮单击应用程序崩溃,与 Storyboard绑定(bind)

标签 ios swift uibutton selector swift3

我在 Swift 2.2 中开发了一个项目,在 Swift 3 启动后我需要在 Swift 3 中转换它并使其可与 iOS 10 编译。但是在转换项目之后我遇到了一些问题,比如当我点击与 Storyboard连接的按钮时我的应用程序崩溃,原因是 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)
              }
         }

最佳答案

看到这个

@@IBAction func btnTwitter_Clicked(sender:UIButton) 你在按钮前面添加了额外的 @,检查一次,

在swift3中我们需要像这样写按钮 Action

yourbuttonName.addTarget(self, action: #selector(yourVCName. btnTwitter_Clicked(_:)), for: .touchUpInside)

和调用方法一样

@IBAction func btnTwitter_Clicked(_ sender: UIButton){
   print("Button pressed 👍 ")
  // continue your work

 }

或者把按钮Outlets和IBActions删掉,重新生成一次,肯定有效

关于ios - 在 swift 3 中,按钮单击应用程序崩溃,与 Storyboard绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39739079/

相关文章:

ios - 以编程方式更改堆栈 View 中容器的高度

在 UIPanGestureRecognizer 中快速获取按钮标题

ios - iOS 中设置 CAGradient UIButton 的选中状态

iOS Push UIViewController 像 Snapchat 这样的滑动手势

ios - 有没有办法阻止用户在从 Firebase 发送后接收推送通知?

swift - 如何在 tvOS 中授权 OAuth2

ios - Swift 3.0 FileManager.fileExists(atPath :) always return false

ios - UIButton 和 UIImage 。将按钮的图像设置为零后更改框架不会删除图像

ios - 比较日期组件 Swift

ios - 重新加载 UITableView 上未显示的数据