I have coded a custom UIButton as :
class AccountOpeningButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
......
}
}
I am able to instantiate this Class successfully using my Storyboard. Now, i made a UIView & want to add this button in my UIView as :
var customView:UIView = UIView()
customView.frame = CGRect(x: 0, y: 0, width: 350, height: 250)
.....
let fromDateBtn:UIButton = AccountOpeningButton()//Error comes here as : Missing Argument for parameter ‘coder’ in call
customView.addSubview(fromDateBtn)
So please help in in reusing this code dynamically also.
P.S. : I referred http://napora.org/nscoder-and-swift-initialization/ Fatal error: use of unimplemented initializer 'init(coder:)' for class Class does not implement its superclass's required members But didn't succeed.
======================================================================= TRIED
let fromDateBtn:UIButton = UIButton() as! AccountOpeningButton
This throws CastException Could not cast value of type 'UIButton' to '.AccountOpeningButton'
Replace This line
With This:
And add this method in your class
For more details find Here