使用按钮更改 UIAlertView 内容

Change UIAlertView content with buttons

我创建了一个 UIAlertView。主要内容加载 JSON,我希望当我单击 "Completo" button 时,内容更改为另一个变量值 completo。当我点击 Mapa 时,我需要打开一张地图。当我单击 Atrás 时,它应该会关闭警报(现在所有按钮都会关闭它)。

var tabla: String = "" // JSON Data
...

@IBAction func Itinerario(sender: AnyObject) {

    let alert = UIAlertView(title: "Itinerario de Procesión", message: (tabla), delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "Completo", "Mapa", "Atrás")

    alert.show()

}

我还想更改按钮颜色。

我得到了这个建议:/Users/Javi/Desktop/ElPenitente/El Penitente/Domingo R.swift:87:21: 'UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

所以错误的意思是你应该使用 UIAlertController 来代替。

用法非常简单:

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let action1 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
let action2 = UIAlertAction(title: "Action", style: UIAlertActionStyle.Default) { (action) in
    // Do something here when action was pressed
}

alert.addAction(action1)
alert.addAction(action2)

self.presentViewController(alert, animated: true, completion: nil)

其中 action1 具有系统样式 .Cancel 且没有处理程序,action2 具有默认样式和处理程序。

可以使用视图的 tintColor 属性更改操作的颜色,请参阅