【问题标题】:Missing Argument for parameter ‘coder’ in call调用中缺少参数“编码器”的参数
【发布时间】:2016-06-14 18:49:21
【问题描述】:

我将自定义 UIButton 编码为:

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

我能够使用我的 Storyboard 成功地实例化这个类。 现在,我制作了一个 UIView 并想在我的 UIView 中添加这个按钮:

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)

所以请帮忙动态重用这段代码。

附: : 我推荐了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 但没有成功。

================================================ ========================= 尝试过

let fromDateBtn:UIButton = UIButton() as! AccountOpeningButton

这会引发 CastException 无法将“UIButton”类型的值转换为“.AccountOpeningButton”

【问题讨论】:

    标签: ios iphone swift uiview uibutton


    【解决方案1】:

    替换此行

    let fromDateBtn:UIButton = AccountOpeningButton()
    

    有了这个:

    let fromDateBtn = AccountOpeningButton()
    

    并在你的类中添加这个方法

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

    你可以有多个 init 方法,但你必须遵守 继承和层次结构规则。你需要明确了解 什么叫做便利初始化器。

    更多详情请联系Here

    【讨论】:

    • 接受答案。 (单击复选标记。)投票是可选的。接受正确答案不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多