如何初始化一个 UIButton 子类? [英] how to init a UIButton subclass?
问题描述
我正在尝试在 Swift 中向 UIButton 的子类添加双精度值尝试了所有类型的初始化以及获取和设置选项,但我无法让它工作.
所以我从这个开始
class CVSTButton : UIButton {var cvstPosition: 双必需的 init(coder aDecoder: NSCoder) {fatalError("init(coder:) 尚未实现")super.init(编码器:aDecoder)}}
然后我尝试了:
class CVSTButton : UIButton {var cvstPosition:双{得到 {返回 self.cvstPosition}放 {self.cvstPosition = newValue}}必需的 init(coder aDecoder: NSCoder) {fatalError("init(coder:) 尚未实现")super.init(编码器:aDecoder)}}
我不明白这里有什么问题....请帮忙...
使用 Swift 3,您可以根据您的需要,选择以下七个代码片段之一来解决您的问题.><小时>
1.使用自定义初始化程序创建您的 UIButton
子类
此解决方案允许您使用适当的属性值创建 UIButton
子类的实例.使用此解决方案,您只能以编程方式创建 UIButton 子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: Int所需的初始化(值:Int = 0){//在调用 super.init 之前设置 myValueself.myValue = 价值super.init(帧:.零)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}}
用法:
导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让按钮 = 自定义按钮(值:0)//let button = CustomButton()//也有效button.setTitle("Hello", for: .normal)//自动布局button.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(按钮)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = trueprint(button.myValue)//打印 0}}
<小时>
2.使用便利的初始值设定项创建您的 UIButton
子类
此解决方案允许您使用适当的属性值创建 UIButton
子类的实例.使用此解决方案,您只能以编程方式创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: Int方便初始化(squareOf值:Int){self.init(值:值 * 值)}所需的初始化(值:Int = 0){//在调用 super.init 之前设置 myValueself.myValue = 价值super.init(帧:.零)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}}
用法:
导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让按钮 = CustomButton(squareOf: 10)//let button = CustomButton(value: 100)//也可以button.setTitle("Hello", for: .normal)//自动布局button.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(按钮)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = trueprint(button.myValue)//打印 100}}
<小时>
3.使用 init(frame: CGRect)
初始化器
创建您的 UIButton
子类使用此解决方案,您只能以编程方式创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: Int覆盖初始化(框架:CGRect){//在调用 super.init 之前设置 myValueself.myValue = 0super.init(框架:框架)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器aDecoder:NSCoder){fatalError("init(coder:) 尚未实现")}}
用法:
导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让按钮 = CustomButton(frame: .zero)//let button = CustomButton()//也可以button.setTitle("Hello", for: .normal)//自动布局button.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(按钮)button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truebutton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = trueprint(button.myValue)//打印 0}}
<小时>
4.使用 init?(coder aDecoder: NSCoder)
初始化器
创建您的 UIButton
子类使用此解决方案,您可以从 Storyboard 创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: Int需要初始化?(编码器aDecoder:NSCoder){//在调用 super.init 之前设置 myValueself.myValue = 0super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
用法:
导入 UIKit类视图控制器:UIViewController {@IBOutlet 弱变量按钮:CustomButton!覆盖 func viewDidLoad() {super.viewDidLoad()print(button.myValue)//打印 0}}
<小时>
5.使用 init(frame: CGRect)
和 init?(coder aDecoder: NSCoder)
初始值设定项
创建您的 UIButton
子类使用此解决方案,您可以以编程方式或从 Storyboard 创建 UIButton
子类的实例.
导入 UIKit类自定义按钮:UIButton {var myValue: Int覆盖初始化(框架:CGRect){//在调用 super.init 之前设置 myValueself.myValue = 0super.init(框架:框架)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器aDecoder:NSCoder){//在调用 super.init 之前设置 myValueself.myValue = 0super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
<小时>
6.使用您的属性的默认属性值创建您的 UIButton
子类
作为先前解决方案的替代方案,您可以在初始值设定项之外为您的属性分配一个初始值.
导入 UIKit类自定义按钮:UIButton {var myValue: Int = 0覆盖初始化(框架:CGRect){super.init(框架:框架)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器aDecoder:NSCoder){super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
<小时>
7.使用具有可选类型的属性创建 UIButton
子类
如果您不想/不能在创建按钮时为您的属性设置默认值,则必须将您的属性类型设置为可选.
导入 UIKit类自定义按钮:UIButton {var myValue: 整数?= 零覆盖初始化(框架:CGRect){super.init(框架:框架)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}需要初始化?(编码器aDecoder:NSCoder){super.init(编码器:aDecoder)//如果需要,在 super.init 之后设置其他操作背景颜色 = .red}}
I am trying to add a double value to a subclass of UIButton in Swift Tried all kind of inits and get and set options but I can'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)
}
}
I do not get whats wrong here.... please help...
With Swift 3, according to your needs, you may choose one of the seven following code snippets to solve your problem.
1. Create your UIButton
subclass with a custom initializer
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
}
}
2. Create your UIButton
subclass with a convenience initializer
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
}
}
3. Create your UIButton
subclass with init(frame: CGRect)
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
}
}
4. Create your UIButton
subclass with init?(coder aDecoder: NSCoder)
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
}
}
5. Create your UIButton
subclass with init(frame: CGRect)
and init?(coder aDecoder: NSCoder)
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
}
}
6. Create your UIButton
subclass with a default property value for your property
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
}
}
7. Create your UIButton
subclass with your property having an optional type
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 子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!