使用Swift 3,您可以根据需要选择以下七个代码段之一来解决问题。
1.UIButton
使用自定义初始化程序创建子类
此解决方案允许您UIButton
使用属性的适当值创建子类的实例。使用此解决方案,您只能以编程方式创建UIButton子类的实例。
import UIKit
class CustomButton: UIButton {
var myValue: Int
required init(value: Int = 0) {
self.myValue = value
super.init(frame: .zero)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
用法:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(value: 0)
button.setTitle("Hello", for: .normal)
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)
}
}
2.UIButton
使用便捷初始化程序创建子类
此解决方案允许您UIButton
使用属性的适当值创建子类的实例。使用此解决方案,您只能以UIButton
编程方式创建子类的实例。
import UIKit
class CustomButton: UIButton {
var myValue: Int
convenience init(squareOf value: Int) {
self.init(value: value * value)
}
required init(value: Int = 0) {
self.myValue = value
super.init(frame: .zero)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
用法:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(squareOf: 10)
button.setTitle("Hello", for: .normal)
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)
}
}
3.UIButton
使用init(frame: CGRect)
初始化程序创建子类
使用此解决方案,您只能以UIButton
编程方式创建子类的实例。
import UIKit
class CustomButton: UIButton {
var myValue: Int
override init(frame: CGRect) {
self.myValue = 0
super.init(frame: frame)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
用法:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton(frame: .zero)
button.setTitle("Hello", for: .normal)
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)
}
}
4.UIButton
使用init?(coder aDecoder: NSCoder)
初始化程序创建子类
使用此解决方案,您可以UIButton
从Storyboard创建子类的实例。
import UIKit
class CustomButton: UIButton {
var myValue: Int
required init?(coder aDecoder: NSCoder) {
self.myValue = 0
super.init(coder: aDecoder)
backgroundColor = .red
}
}
用法:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: CustomButton!
override func viewDidLoad() {
super.viewDidLoad()
print(button.myValue)
}
}
5.UIButton
使用init(frame: CGRect)
和init?(coder aDecoder: NSCoder)
初始化程序创建子类
使用此解决方案,您可以以UIButton
编程方式或从Storyboard创建子类的实例。
import UIKit
class CustomButton: UIButton {
var myValue: Int
override init(frame: CGRect) {
self.myValue = 0
super.init(frame: frame)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
self.myValue = 0
super.init(coder: aDecoder)
backgroundColor = .red
}
}
6.UIButton
使用属性的默认属性值创建子类
作为先前解决方案的替代方法,您可以在初始化程序之外为属性分配一个初始值。
import UIKit
class CustomButton: UIButton {
var myValue: Int = 0
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
backgroundColor = .red
}
}
7.UIButton
使用具有可选类型的属性创建子类
如果您不想/无法在创建按钮时为属性设置默认值,则必须将属性类型设置为可选。
import UIKit
class CustomButton: UIButton {
var myValue: Int? = nil
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
backgroundColor = .red
}
}