gpt4 book ai didi

swift - 如何将具有自定义指定初始化程序的 UISegmentedControl 子类化?

转载 作者:搜寻专家 更新时间:2023-10-31 23:08:19 25 4
gpt4 key购买 nike

这似乎是一个微不足道的问题,但我无法编译。无论是在 Playground 上还是在正常的 swift ios 项目中。(请注意,我没有使用 Storyboard,这就是为什么我不需要/关心 init?(coder) 部分..it;s 只是必须包含它,否则编译器会提示它。)

class SegmentedControl: UISegmentedControl {    
let configuration: [String]
required init(configuration: [String]) {
self.configuration = configuration
super.init(items: configuration)
}
required init?(coder aDecoder: NSCoder) { fatalError() }
}
let x = SegmentedControl(configuration: ["a","b"])

它提示没有实现指定的初始化程序。

Fatal error: Use of unimplemented initializer 'init(frame:)' for class '__lldb_expr_167.SegmentedControl'

我不明白这是怎么回事。指定初始化器不是 UISegmentedControl 的 init(items:) 吗?我在我的子类指定初始化器中调用它。

最佳答案

解决方法:

class SegmentedControl: UISegmentedControl {
var configuration = [String]()
required init(configuration: [String]) {
super.init(items: configuration)
self.configuration = configuration
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) { fatalError() }
}
let x = SegmentedControl(configuration: ["a","b"])

关于swift - 如何将具有自定义指定初始化程序的 UISegmentedControl 子类化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734345/

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