“无法识别的选择器发送到实例"迅速 [英] "Unrecognized selector sent to instance" in swift

查看:42
本文介绍了“无法识别的选择器发送到实例"迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我做错了什么.我对编程也很陌生,所以我不太擅长调试.这是一个测试应用程序,因此我可以看到与应用程序开发的联系有多快.到目前为止,我得到了这个:

I have no idea what I am doing wrong. I am also quite new to programming so I am not very good at debugging. This was a test app so that I can see how swift ties in with app development. So far, I have got this:

class ViewController: UIViewController, UITextViewDelegate {

    var textView: UITextView!

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var widthField = self.view.bounds.size.width - 10
        var heightField = self.view.bounds.size.height - 69 - 221
        var textFieldString: String! = ""
        //Set up text field
        self.textView = UITextView(frame: CGRectMake(5, 64, widthField, heightField))
        self.textView.backgroundColor = UIColor.redColor()
        self.view.addSubview(self.textView)
        //Set up the New button
        var newButtonString: String! = "New Note"
        var heightButton = 568 - heightField - 1000
        let newButton = UIButton(frame: CGRect(x: 5, y: 5, width: widthField, height: 50)) as UIButton
        UIButton.buttonWithType(UIButtonType.System)
        newButton.setTitle(newButtonString,forState: UIControlState.Normal)
        newButton.backgroundColor = UIColor.redColor()
        newButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(newButton)

    }

    func buttonAction() {
        println("tapped button")
    }
}

当我按下 iOS 模拟器中的按钮时,我收到错误Unrecognized selector sent to instance".应用程序打开正常,但只要按下按钮,它就会崩溃.

I am getting the error "Unrecognized selector sent to instance" when I press the button in the iOS simulator. The app opens fine but whenever the button is pressed, it just crashes.

推荐答案

func buttonAction(){...

应该是

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

因为 newButton 的 action: "buttonAction:" 所以.

这篇关于“无法识别的选择器发送到实例"迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆