0

I created a subclass of UIButton:

import UIKit

@IBDesignable
class CheckboxButton: UIButton {

@IBInspectable var checkboxBackgroundColor: UIColor = Project.Color.baseGray
@IBInspectable var textColor: UIColor = Project.Color.mainDarkText

@IBInspectable var checkboxHighlightedBackgroundColor: UIColor = Project.Color.main
@IBInspectable var highlightedTextColor: UIColor = Project.Color.mainBrightText

// MARK: - Properties
var isChecked: Bool = false {
    didSet {
        changeState()
    }
}


// MARK: - Overrides
override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupView()
}

override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {

    if isChecked {
        isChecked = false
    } else {
        isChecked = true
    }

    return false
}

override func prepareForInterfaceBuilder() {
    changeState()
}


// MARK: - @IBActions



// MARK: - Functions
private func setupView() {
    isUserInteractionEnabled = true
    changeState()
}

private func changeState() {
    if isChecked {
        backgroundColor = checkboxHighlightedBackgroundColor
        self.setTitleColor(highlightedTextColor, for: .normal)
    } else {
        backgroundColor = checkboxBackgroundColor
        self.setTitleColor(textColor, for: .normal)
    }
}
 }

Now I added a button inside the storyboard and gave it the class CheckboxButton. Everything works. Then I added an IBAction like this:

@IBAction func pointBtnTapped(_ sender: CheckboxButton) {
    print("tapped")
    selectButton(withNumber: sender.tag)
}

But this doesn't work (nothing prints out). It works with a normal button, but not if the button is the subclass CheckboxButton. Do you have any ideas?

Edit: screenshots https://www.dropbox.com/s/ewicc349ag9l6y2/Screenshot%202016-10-06%2023.41.39.png?dl=0

https://www.dropbox.com/s/vxevzscueproivc/Screenshot%202016-10-06%2023.42.33.png?dl=0 (couldn't embed them here. Don't know why)

Thank you!

SwiftedMind
  • 3,701
  • 3
  • 29
  • 63
  • Does it work if you change the declared type of `sender` to `AnyObject`? – BaseZen Oct 06 '16 at 21:35
  • 1
    And when you say it doesn't work, does it crash, print a warning/error, or fail silently? Also, if you change the type or name of a button in Storyboard, you have to redraw the `IBAction` connection and delete any stale ones. Does the little circle next to the method show as filled in? – BaseZen Oct 06 '16 at 21:37
  • No, `AnyObject`doesn't work. I mean, nothing happens. It doesn't crash, it just doesn't print anything. Sorry, if my text was misleading. I checked the circle, it's filled in – SwiftedMind Oct 06 '16 at 21:38
  • Probably we'll need screen captures of the Attributes Inspector, Identity Inspector, and Outlets Inspector of the button in question. – BaseZen Oct 06 '16 at 21:40
  • 1
    You have overridden `beginTracking` and returned false, so the control won't track the touch beyond the initial touch down inside. – Paulw11 Oct 06 '16 at 21:45
  • That could be it. One second, I'll try EDIT: it worked :) You wanna post this as answer? I'll accept it – SwiftedMind Oct 06 '16 at 21:46
  • 1
    Also, touchUpInside is probably the wrong action to use with this sort of control. `valueChanged` would be more appropriate; you will need to fire this action yourself. – Paulw11 Oct 06 '16 at 21:47

1 Answers1

1

You broke UIButton by overriding beginTracking() and always returning false. This brainwashes the button into thinking it's never being clicked.

What was your intent there? In any case, return true there and your code will fire the @IBAction.

EDIT: You're overthinking the issue by using a low-level method meant for highly customized behavior such as non-rectangular buttons. You just need:

How to use UIButton as Toggle Button?

Community
  • 1
  • 1
BaseZen
  • 8,650
  • 3
  • 35
  • 47