呼叫中参数“编码器"的参数丢失 [英] Missing Argument for parameter ‘coder’ in call

查看:119
本文介绍了呼叫中参数“编码器"的参数丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将自定义UIButton编码为:

I have coded a custom UIButton as :

 class AccountOpeningButton: UIButton {
  required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        ......
   }
}

我可以使用我的故事板成功实例化该类. 现在,我做了一个UIView&想要在我的UIView中将此按钮添加为:

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. :我提到了 http://napora.org/nscoder-and-swift-initialization/ 致命错误:使用未实现的初始化程序"init(coder :)"上课 类未实现其超类的必需成员 但是没有成功.

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

这将引发CastException 无法将类型为'UIButton'的值强制转换为'.AccountOpeningButton'

This throws CastException Could not cast value of type 'UIButton' to '.AccountOpeningButton'

推荐答案

替换此行

let fromDateBtn:UIButton = AccountOpeningButton()

与此:

let fromDateBtn = AccountOpeningButton()

并在您的班级中添加此方法

And add this method in your class

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

您可以使用多个init方法,但必须遵守 继承和层次结构规则.而且您需要明确了解 所谓的便捷初始化器.

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

这篇关于呼叫中参数“编码器"的参数丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆