ios - Swift 无法识别的选择器发送到实例 - 我错过了什么?

标签 ios swift

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

    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() 方法中。将它移到外面,它应该可以到达。

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

关于ios - Swift 无法识别的选择器发送到实例 - 我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25275694/

相关文章:

swift - 使用 Alamofireimage 从服务器获取图像后,Imageview 未更新

swift - Swift 中的好友类(访问内部类的私有(private)成员)

ios - 如何获得两个日期之间的天数 objective-c

ios - 如何防止调整 UIView 的大小以适应 ScrollView 的高度(禁用自动调整)?

ios - 获取对象的内存

ios - 将 UIDatePicker countDownTimer 默认值设置为 1 分钟

xcode - 我将如何以编程方式快速引用我的应用程序中的主 Storyboard?

ios - 自定义 UIGestureRecognizer 并不总是调用其操作方法

ios - 覆盖数组swift ios中的旧对象

ios - 静默通知不会在 TestFlight 中发送