0

I am creating a class to manage the checkboxes.

This is what I did:

import UIKit

class CheckboxButton: UIButton {

    //let checked = ""
    //let unchecked = ""

    let checked = "bubu"
    let unchecked = "baba"

    var isChecked:Bool = false{
        didSet{
            if isChecked == true {
                self.setTitle(checked, forState: UIControlState.Normal)
            }else{
                self.setTitle(unchecked, forState: UIControlState.Normal)
            }
        }
    }

    override func awakeFromNib() {
        self.titleLabel?.font = UIFont(name: "FontAwesome", size: 20)
        self.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        self.isChecked = false
    }

    func buttonClicked(sender:UIButton){
        if(sender == self){
            if isChecked == true {
                isChecked = false
            }else{
                isChecked = true
            }
        }
    }
}

Everything is fine, but when I click on the button, the app just crashes with error Thread 1: signal SIGABRT

Is there something wrong?

Thanks for any help!

jww
  • 97,681
  • 90
  • 411
  • 885
Anonymous
  • 1,021
  • 2
  • 10
  • 24

1 Answers1

0

Since your action function takes parameters func buttonClicked(sender:UIButton) the action of your target should always contain : at the end, so all you have to do is to replace add target line with this

self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

Also your buttonClicked function can be simplified like this:

func buttonClicked(sender:UIButton){
    if(sender == self){
        isChecked = !isChecked
    }
}
Zell B.
  • 10,266
  • 3
  • 40
  • 49