Swift unrecognized selector sent to instance error

北城余情 提交于 2019-11-29 16:41:53

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!) { ... 

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)
vichhai

For Swift 2.2 with Xcode 8:

self.followedButton.addTarget(parentView, action: #selector(CustomCell.followButtonClick(_:)), forControlEvents: .TouchUpInside)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!