问题描述
我尝试在 Swift 中向 UIButton 的子类添加双精度值.我尝试了所有类型的初始化以及获取和设置选项,但都无法正常工作.
所以我从这个开始:
类 CVSTButton : UIButton {var cvstPosition: 双需要初始化(编码器aDecoder:NSCoder){致命错误(初始化(编码器:)尚未实现")super.init(编码器:aDecoder)}}
然后我尝试了:
类 CVSTButton : UIButton {var cvstPosition: 双 {得到 {返回 self.cvstPosition}放 {self.cvstPosition = newValue}}需要初始化(编码器aDecoder:NSCoder){致命错误(初始化(编码器:)尚未实现")super.init(编码器:aDecoder)}}
这里有什么问题?
使用 Swift 3,您可以根据需要选择以下七个代码片段之一来解决您的问题.p><小时>
子类
此解决方案允许您使用适合您的属性的值创建 UIButton
子类的实例.使用此解决方案,您只能以编程方式创建 UIButton 子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数所需的初始化(值:Int = 0){//在调用 super.init 之前设置 myValueself.myValue = 价值super.init(帧:.zero)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器 aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}}
用法:
导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让按钮 = 自定义按钮(值:0)//let button = CustomButton()//也可以button.setTitle("你好", for: .normal)//自动布局button.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(按钮)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = trueprint(button.myValue)//打印 0}}
<小时>
子类
此解决方案允许您使用适合您的属性的值创建 UIButton
子类的实例.使用此解决方案,您只能以编程方式创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数便利 init(squareOf value: Int) {self.init(值:值 * 值)}所需的初始化(值:Int = 0){//在调用 super.init 之前设置 myValueself.myValue = 价值super.init(帧:.zero)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器 aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}}
用法:
导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让按钮 = CustomButton(squareOf: 10)//let button = CustomButton(value: 100)//也可以button.setTitle("你好", for: .normal)//自动布局button.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(按钮)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = trueprint(button.myValue)//打印 100}}
<小时>
初始化程序
创建您的UIButton
子类使用此解决方案,您只能以编程方式创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数覆盖初始化(帧:CGRect){//在调用 super.init 之前设置 myValueself.myValue = 0超级初始化(帧:帧)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器 aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}}
用法:
导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让按钮 = CustomButton(frame: .zero)//let button = CustomButton()//也可以button.setTitle("你好", for: .normal)//自动布局button.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(按钮)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = trueprint(button.myValue)//打印 0}}
<小时>
初始化器
创建您的UIButton
子类使用此解决方案,您可以从 Storyboard 创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数需要初始化?(编码器 aDecoder:NSCoder){//在调用 super.init 之前设置 myValueself.myValue = 0super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
用法:
导入 UIKit类视图控制器:UIViewController {@IBOutlet 弱 var 按钮:CustomButton!覆盖 func viewDidLoad() {super.viewDidLoad()print(button.myValue)//打印 0}}
<小时>
子类
使用此解决方案,您可以通过编程方式或从 Storyboard 创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数覆盖初始化(帧:CGRect){//在调用 super.init 之前设置 myValueself.myValue = 0超级初始化(帧:帧)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器 aDecoder:NSCoder){//在调用 super.init 之前设置 myValueself.myValue = 0super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
<小时>
子类
作为先前解决方案的替代方案,您可以在初始化程序之外为您的属性分配一个初始值.
导入 UIKit类自定义按钮:UIButton {var myValue: Int = 0覆盖初始化(帧:CGRect){超级初始化(帧:帧)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器 aDecoder:NSCoder){super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
<小时>
子类
如果您不想/不能在创建按钮时为您的属性设置默认值,则必须将您的属性类型设置为可选.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数?= 无覆盖初始化(帧:CGRect){超级初始化(帧:帧)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器 aDecoder:NSCoder){super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
I tried to add a double value to a subclass of UIButton in Swift. I tried all kind of inits and get and set options, but I couldn’t get it to work.
So I started with this:
class CVSTButton : UIButton {
var cvstPosition: Double
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
}
Then I tried:
class CVSTButton : UIButton {
var cvstPosition: Double {
get {
return self.cvstPosition
}
set {
self.cvstPosition = newValue
}
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
}
What’s is wrong here?
With Swift 3, according to your needs, you may choose one of the seven following code snippets to solve your problem.
subclasswithacustominitializer
This solution allows you to create instances of your UIButton
subclass with the appropriate value for your property. With this solution, you can only create instances of your UIButton subclass programmatically.
import UIKit
class CustomButton: UIButton {
var myValue: Int
required init(value: Int = 0) {
// set myValue before super.init is called
self.myValue = value
super.init(frame: .zero)
// set other operations after super.init, if required
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Usage:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(value: 0)
// let button = CustomButton() // also works
button.setTitle("Hello", for: .normal)
// auto layout
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
print(button.myValue) // prints 0
}
}
subclasswithaconvenienceinitializer
This solution allows you to create instances of your UIButton
subclass with the appropriate value for your property. With this solution, you can only create instances of your UIButton
subclass programmatically.
import UIKit
class CustomButton: UIButton {
var myValue: Int
convenience init(squareOf value: Int) {
self.init(value: value * value)
}
required init(value: Int = 0) {
// set myValue before super.init is called
self.myValue = value
super.init(frame: .zero)
// set other operations after super.init, if required
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Usage:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(squareOf: 10)
// let button = CustomButton(value: 100) // also works
button.setTitle("Hello", for: .normal)
// auto layout
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
print(button.myValue) // prints 100
}
}
initializer
With this solution, you can only create instances of your UIButton
subclass programmatically.
import UIKit
class CustomButton: UIButton {
var myValue: Int
override init(frame: CGRect) {
// set myValue before super.init is called
self.myValue = 0
super.init(frame: frame)
// set other operations after super.init, if required
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Usage:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(frame: .zero)
//let button = CustomButton() // also works
button.setTitle("Hello", for: .normal)
// auto layout
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
print(button.myValue) // prints 0
}
}
initializer
With this solution, you can create instances of your UIButton
subclass from Storyboard.
import UIKit
class CustomButton: UIButton {
var myValue: Int
required init?(coder aDecoder: NSCoder) {
// set myValue before super.init is called
self.myValue = 0
super.init(coder: aDecoder)
// set other operations after super.init, if required
backgroundColor = .red
}
}
Usage:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: CustomButton!
override func viewDidLoad() {
super.viewDidLoad()
print(button.myValue) // prints 0
}
}
initializers
With this solution, you can create instances of your UIButton
subclass programmatically or from Storyboard.
import UIKit
class CustomButton: UIButton {
var myValue: Int
override init(frame: CGRect) {
// set myValue before super.init is called
self.myValue = 0
super.init(frame: frame)
// set other operations after super.init, if required
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
// set myValue before super.init is called
self.myValue = 0
super.init(coder: aDecoder)
// set other operations after super.init if required
backgroundColor = .red
}
}
subclasswithadefaultpropertyvalueforyourproperty
As an alternative to the previous solutions, you can assign an initial value to your property outside of the initializers.
import UIKit
class CustomButton: UIButton {
var myValue: Int = 0
override init(frame: CGRect) {
super.init(frame: frame)
// set other operations after super.init, if required
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// set other operations after super.init if required
backgroundColor = .red
}
}
subclasswithyourpropertyhavinganoptionaltype
If you don't want to / can't set a default value to your property when your button is created, you must set your property type as an optional.
import UIKit
class CustomButton: UIButton {
var myValue: Int? = nil
override init(frame: CGRect) {
super.init(frame: frame)
// set other operations after super.init, if required
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// set other operations after super.init if required
backgroundColor = .red
}
}
这篇关于如何初始化 UIButton 子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!