我想知道如何为我的Alert.swift自定义类中声明的枚举设置一个值,以便按钮更改样式。到目前为止,这是我的代码:
var alert: Alert!
在我的viewDidLoad中创建警报
alert = Alert(frame: CGRect(x: 0, y: 0, width: self.view.frame.width , height: 60), buttonType: .Uno, disabled: true)
我有一个自定义类Alert.swift,在其中声明了枚举:
enum ButtonType {
case Cero
case Uno
case Dos
case Tres
}
然后在初始化程序中:
convenience init(frame: CGRect, buttonType: ButtonType, disabled: Bool = false) {
self.init(frame: frame)
switch buttonType {
case .Uno:
rightButton = UIButton(type: UIButtonType.System) as UIButton
rightButton.frame = CGRectMake(self.alertView.frame.width-60, 0, 60, 60)
rightButton.backgroundColor = UIColor.greenColor()
rightButton.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
rightButton.setImage(UIImage(named: "rightButtonImage"), forState: UIControlState.Normal)
rightButton.imageView?.contentMode = UIViewContentMode.Center
alertView.addSubview(rightButton)
...
我的问题是,一旦创建警报
alert = Alert(frame: CGRect(x: 0, y: 0, width: self.view.frame.width , height: 60), buttonType: .Uno, disabled: true)
如果我想更改按钮样式,可以说是这样的:
alert.buttonType = .Dos
我不知道如何在Alert.swift类中进行管理,我只知道如何在创建警报时设置样式时设置样式。提前致谢!!
在您的Alert
班级中添加以下内容:
class Alert {
var buttonType: ButtonType {
didSet {
configureForButtonType()
}
}
private func configureForButtonType() {
switch buttonType {
case .Uno:
...
break
case .Cero:
...
break
case .Dos:
...
break
case .Tres:
...
break
}
}
}
将所有样式从头switch
开始buttonType
放入configureForButtonType()
方法中。buttonType
在初始化时设置时,该类将自行配置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句