本文介绍了Swift无法识别的选择器发送到实例 - 我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看不出为什么以下按钮抛出错误:2014-08-13 00:29:57.674 view1 [26581:842344] - [view1.ViewController buttonAction:]:无法识别的选择器发送到实例0x797731c0
2014-08-13 00:29:57.677 view1 [26581:842344] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [view1.ViewController buttonAction:]:无法识别的选择器发送到实例0x797731c0'。任何帮助赞赏。非常感谢

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 instance 0x797731c02014-08-13 00:29:57.677 view1[26581:842344] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[view1.ViewController buttonAction:]: unrecognized selector sent to instance 0x797731c0'. Any help appreciated. Many Thanks

    import UIKit

        class ViewController: UIViewController {

        override func viewDidLoad() {
        super.viewDidLoad()


        func buttonAction(sender:UIButton)
        {
            println("Button tapped")
        }

        let windowsize = CGRect(x: 0, y: 0, width: 320, height: 480)
        let labelsize = CGRect(x: 0, y: 200, width: 140, height: 60)
        let myButton = UIButton(frame: CGRectMake(0, 0, 100, 100))



        let myView = UIView(frame: windowsize)
        let viewwidth = myView.frame.width
        let spaceleft = (viewwidth - labelsize.width)/2

        myView.backgroundColor = UIColor .blackColor()
        let myLabel = UILabel(frame:labelsize)
        myLabel.frame.origin.x = spaceleft
        myLabel.text = "Hello"
        myLabel.font = UIFont(name: "Chalkduster", size: 22)
        myLabel.textColor = UIColor .whiteColor()
        myLabel.textAlignment = NSTextAlignment.Center


        myLabel.backgroundColor = UIColor .redColor()
        //myView.addSubview(myButton)

        myView.addSubview(myLabel)
        self.view.addSubview(myView)



        let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        myView.addSubview(button)



        // Do any additional setup after loading the view, typically from a nib.
    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


推荐答案

您的 buttonAction 目标嵌套在 viewDidLoad()方法中。将它移到外面它应该是可以访问的。

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

这篇关于Swift无法识别的选择器发送到实例 - 我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:19