Unable to subclass UIButton: must call the designated initializer of the superclass 'UIButton'

An attempt to subclass UIButton, but an error occurs Must call a designated initializer of the superclass 'UIButton'.

Studying a few SO posts like this , this , this, or a few others did not help, as these solutions did not work.

How can we subclass UIButtonin Swift and define a custom init function?

import UIKit

class KeyboardButton : UIButton {
    var letter = ""
    var viewController:CustomViewController?

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

    init(letter: String, viewController: CustomViewController) {
        super.init()
        ...
    }
}
+4
source share
1 answer

You must call the initializer of the assigned superclass:

Swift 3 and 4:

init(letter: String, viewController: CustomViewController) {
    super.init(frame: .zero)
}

Swift 1 and 2:

init(letter: String, viewController: CustomViewController) {
    super.init(frame: CGRectZero)
}

Paulw11, , , , .

+7

All Articles