Swift unrecognized selector sent to instance error

前端 未结 3 1279
渐次进展
渐次进展 2020-12-21 21:05

I recently converted my project from Objective-C to Swift and in doing so I acquired this error whenever I click a button in the table view\'s cell. I have multiple cells be

相关标签:
3条回答
  • 2020-12-21 21:50

    For Swift 2.2 with Xcode 8:

    self.followedButton.addTarget(parentView, action: #selector(CustomCell.followButtonClick(_:)), forControlEvents: .TouchUpInside)
    
    0 讨论(0)
  • 2020-12-21 21:51

    For Swift3, you need to change the following:

    self.followedButton.addTarget(parentView, action: Selector(("followedButtonClick")), for: .touchUpInside)
    

    With:

    self.followedButton.addTarget(parentView, action: #selector(self.followButtonClick(_:)), forControlEvents: .touchUpInside)
    
    0 讨论(0)
  • 2020-12-21 22:05

    Two changes for Swift 3:

    The selector should look like:

    #selector(ClassName.followButtonClick(_:))
    

    The function should have an underscore:

    @IBAction func followButtonClick(_ sender: UIButton!) { ...
    

    Notice that these two should be in the same class, otherwise, make sure you initialize the ClassName class.

    If you want the selector method(followButtonClick(_:)) to be in the UITableViewCell class. Remove @IBAction(I don't think you need it there):

    func followButtonClick(_ sender: UIButton!) { ... 
    
    0 讨论(0)
提交回复
热议问题