Anton O.
Anton O.

Reputation: 663

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

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

Upvotes: 1

Views: 338

Answers (1)

Arun Kumar
Arun Kumar

Reputation: 798

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

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

Upvotes: 3

Related Questions