Swift unrecognized selector sent to instance - What am I missing?

前端 未结 1 1083
感情败类
感情败类 2020-12-11 19:15

I can\'t see why the following button is throwing an error:2014-08-13 00:29:57.674 view1[26581:842344] -[view1.ViewController buttonAction:]: unrecognized selector sent to i

相关标签:
1条回答
  • 2020-12-11 19:38

    Your buttonAction target is nested inside your viewDidLoad() method. Move it outside and it should be reachable.

    class ViewController: UIViewController {
    
        func buttonAction(sender:UIButton)
        {
            println("Button tapped")
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // ...
    
            let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
            // ...
            button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
            myView.addSubview(button)
        }
    }
    
    0 讨论(0)
提交回复
热议问题