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

前端 未结 4 1193
一生所求
一生所求 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

    Both following statements work perfectly. The upper one is mostly used. However when the selector method is in a different ViewController the compiler warning "No method declared with Objective-C selector 'buttonHandler'" may occur.

    The second listed statement does not give this warning.

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

    In the target view controller (MainViewController) you can define the module:

    func buttonHandler(sender:UIButton!) {
        print ("Pressed")
    }
    

提交回复
热议问题