Swift required init NSCoder constructor

1.2k views Asked by At

I have the following class representing a button in my iOS 8 custom keyboard:

internal class KeyButton: UIButton {

    required init(char: Character) {
        super.init()
    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Since KeyButton is not initialised via storyboard the constructor (coder: NSCoder) would never be called.

The problem is that I am required to implement (coder: NSCoder) constructor, when I run the app I receive the exception plugin interrupted when instantiating KeyButton.

Why am I required to implement (coder: NSCoder) constructor although I instantiate everything programatically

1

There are 1 answers

0
Grimxn On

It has nothing to do with the coder. UIButton's init() calls init(frame: CGRect), which you haven't implemented. Add the following, and you should be good to go...

override init(frame: CGRect ) {
    super.init(frame: frame)
    println("Button frame allocated")
}