Swift:点击更改UIAlertAction标题

时间:2015-07-10 00:05:02

标签: ios swift uialertcontroller uialertaction

如何在UIAlertAction中引用self

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
        // self.title = String("HELLO")
        print("Handle Ok logic here")
    }))

我希望UIAlertAction的标题在点击时(以及其他内容)发生变化。如何在命令中引用自己,以便在单击时执行操作?

如何点击UIAlertAction并不关闭提醒动作

2 个答案:

答案 0 :(得分:1)

如果要从自己的处理程序访问UIAlertAction对象,只需使用提供给动作处理程序块的action参数。

但是,由于title参数是只读的,因此您无法在创建操作后更改操作的标题。

然而,无论如何,甚至没有理由甚至更改标题,因为在点击任何警报操作按钮时警报总是被取消。

答案 1 :(得分:0)

UIAlertController的title属性是只读的。你不能分配任何东西。但是,你可以处理它以使用此代码。

var propTitle:String = "BYE" // it's property of class

func method(){
    refreshAlert.addAction(UIAlertAction(title: propTitle, style: .Default, handler: { (action: UIAlertAction!) in
        self.propTitle = "HELLO"
    }))
}
相关问题