Why do I get signal SIGABRT when I select a created button?

China☆狼群 提交于 2019-12-11 10:14:08

问题


I'm creating a button in swift 2 and when I select it, I get signal SIGABRT and the app crashes. Heres the code:

    let button = UIButton()//(type: UIButtonType.System) as UIButton!
    button.setTitle("button", forState: .Normal)
    button.setTitleColor(UIColor.blueColor(), forState: .Normal)
    button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(100, 100, 100, 100)
    self.view.addSubview(button)
    func buttonPressed(sender: UIButton!) {
        print("ButtonIsSelected")
    }

It brings me to AppDelegate.swift and in the middle of the NSLog it says: unrecognized selector sent to instance... Please help. Anton


回答1:


func buttonPressed(sender: UIButton!) {
    print("ButtonIsSelected")
}

This method must be in your class body not in function body . As I guess you have done.



来源:https://stackoverflow.com/questions/34477470/why-do-i-get-signal-sigabrt-when-i-select-a-created-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!