如何初始化一个UIButton子类?
我想在Swift的UIButton的子类中添加一个double值尝试所有inits和获取和设置选项,但我不能得到它的工作。
所以我开始这个
class CVSTButton : UIButton { var cvstPosition: Double required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") super.init(coder: aDecoder) } }
然后我试着:
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) } }
我不明白这里有什么问题,请帮忙…
随着Swift 3,根据您的需要,您可以select以下七个代码片段之一来解决您的问题。
1.使用自定义初始化程序创build您的UIButton
子类
这个解决scheme允许你为你的属性创build适当的值的UIButton
子类的实例。 有了这个解决scheme,你只能以编程方式创build你的UIButton子类的实例。
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") } }
用法:
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.用一个方便的初始化程序创build你的UIButton
子类
这个解决scheme允许你为你的属性创build适当的值的UIButton
子类的实例。 有了这个解决scheme,你只能以编程方式创build你的UIButton
子类的实例。
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") } }
用法:
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.用init(frame: CGRect)
初始化器创build你的UIButton
子类
有了这个解决scheme,你只能以编程方式创build你的UIButton
子类的实例。
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") } }
用法:
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.用init?(coder aDecoder: NSCoder)
初始化init?(coder aDecoder: NSCoder)
创build你的UIButton
子类
有了这个解决scheme,你可以从Storyboard创build你的UIButton
子类的实例。
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 } }
用法:
import UIKit class ViewController: UIViewController { @IBOutlet weak var button: CustomButton! override func viewDidLoad() { super.viewDidLoad() print(button.myValue) // prints 0 } }
5.用init(frame: CGRect)
和init?(coder aDecoder: NSCoder)
初始化init?(coder aDecoder: NSCoder)
创build你的UIButton
子类
有了这个解决scheme,你可以编程或从Storyboard创build你的UIButton
子类的实例。
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.创build你的属性的默认属性值的UIButton
子类
作为以前解决scheme的替代方法,您可以在初始化程序之外为您的属性分配一个初始值。
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.用你的属性有一个可选types来创build你的UIButton
子类
如果您不想在您的button创build时为您的属性设置默认值,则必须将您的属性types设置为可选。
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 } }
你需要的两件事情 – (1)在你调用super.init()
之前, cvstPosition
需要一个初始值,无论是在声明中还是在init
。 (2) fatalError
被调用,所以你不要忘记实现初始化程序 – 它基本上是一个特定的崩溃。 删除!
在声明中设置初始值,不需要init
:
class CVSTButton : UIButton { var cvstPosition: Double = 0 }
或者在初始化程序中设置初始值:
class CVSTButton : UIButton { var cvstPosition: Double required init(coder aDecoder: NSCoder) { cvstPosition = 0 super.init(coder: aDecoder) } }
迅速:
override class func buttonWithType(buttonType: UIButtonType) -> AnyObject { let button = super.buttonWithType(buttonType) as! UIButton // your default code return button }
Swift 2:
convenience init(type buttonType: UIButtonType) { super.init(frame: CGRectZero) // this button be automatically .Custom }
Swift 2.2:
只从UIButtoninheritance,而你的子类成为types.Custom
默认。
注:我在XCode 8.3.3中使用Swift 3
这是一个简单和容易的解决方法,我一直在使用时需要添加自定义属性和方法到UIButton
:
class CVSTButton: UIButton { var cvstPosition: Double static func button(withCVSTPosition cvstPosition: Double) -> CVSTButton { let button = CVSTButton(type: .detailDisclosure) // You may adjust the initializer used to suit your needs. button.cvstPosition = cvstPosition // Then you can simply set the the properties (which are passed as arguments to the factor/class method) return button } }
要使用它:
let cvstButton = CVSTButton.button(withCVSTPosition: 2.0)