gpt4 book ai didi

ios - 调用中缺少参数 ‘coder’ 的参数

转载 作者:可可西里 更新时间:2023-11-01 00:36:50 24 4
gpt4 key购买 nike

我已将自定义 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 Could not cast value of type 'UIButton' to '.AccountOpeningButton'

最佳答案

替换这一行

let fromDateBtn:UIButton = AccountOpeningButton()

有了这个:

let fromDateBtn = AccountOpeningButton()

然后在你的类中添加这个方法

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.

有关更多详细信息,请查找 Here

关于ios - 调用中缺少参数 ‘coder’ 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35738383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com