Xcode 7.3 / Swift 2: “No method declared with Objective-C selector” warning

前端 未结 4 1203
一生所求
一生所求 2020-12-05 06:17

I\'ve been using selectors for a while, and even after migrating to Swift I was able to use them without issues. That\'s how I was using on Swift 2 without issues until I up

4条回答
  •  天涯浪人
    2020-12-05 06:58

    Some findings of my own to support what Vincent said (too long to be a direct comment)

    It's not necessarily in a different view controller, but just a different file where the following format won't work:

    button.addTarget(parentViewController, action: Selector("buttonHandler:"), forControlEvents: .TouchUpInside)

    For instance, if you have an extension in a separate file, although for the same view controller, this format Selector("buttonHandler:") won't work.

    Further, when the selector is in the same file and VC, Xcode's quick-fix prompts you to have the selector include the constructor, so it would look something like this:

    #selector(MainViewController.buttonHandler(_:))

    However this format only works when the selector is in the same VC + file, if it is in a separate file, but same VC, then that recommended method won't work, and you need to use the method without the constructor

    #selector(MainViewController.buttonHandler)

提交回复
热议问题