【发布时间】:2020-01-02 17:09:33
【问题描述】:
当我尝试在下面的代码中使用 MySegmentControl 的实例时出现以下错误。该错误在应用程序启动后立即发生。
知道我错过了什么吗?
致命错误:对“TestingSubclassing.MySegmentControl”类使用未实现的初始化程序“init(frame:)”
UISegementedControl 的子类
导入 UIKit
class MySegmentControl: UISegmentedControl {
init(actionName: Selector) {
let discountItems = ["One" , "Two"]
super.init(items: discountItems)
self.selectedSegmentIndex = 0
self.layer.cornerRadius = 5.0
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.blue.cgColor
self.addTarget(self, action: actionName, for: .valueChanged)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
视图控制器
import UIKit
class ViewController: UIViewController {
let segmentOne: MySegmentControl = {
let segment1 = MySegmentControl(actionName: #selector(segmentAction))
return segment1
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(segmentOne)
}
@objc func segmentAction (sender: UISegmentedControl) {
print("segmentAction")
}
}
【问题讨论】:
标签: ios swift uisegmentedcontrol