Missing Argument for parameter ‘coder’ in call

会有一股神秘感。 提交于 2020-02-22 05:35:33

问题


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'


回答1:


Replace This line

let fromDateBtn:UIButton = AccountOpeningButton()

With This:

let fromDateBtn = AccountOpeningButton()

And add this method in your class

override init(frame: CGRect) {
        super.init(frame: frame)
    }

You can have more than one init method, but you have to obey the inheritance and hierarchy rules. And you need to definitely understand what are called convenience initializers.

For more details find Here



来源:https://stackoverflow.com/questions/35738383/missing-argument-for-parameter-coder-in-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!