0

I have created a UIAlertView. The main content is loaded with JSON and I want that when I click "Completo" button, the content change to another variable value completo. When I hit Mapa I need to open a map. And when I click Atrás it should close the alert (right now all the buttons close it).

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()

}

Also I wanted to change buttons color.

I am getting this advice: /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

Javi Rando
  • 115
  • 1
  • 10
  • 1
    Use delegate method `alertView didDismissWithButtonIndex` to do different things upon button click. It is deprecated, which means try to use `UIAlertController`, otherwise some day in the future your code won't work anymore. – zc246 Feb 05 '16 at 09:49
  • That's one of a good advice, I guess. – Fahri Azimov Feb 05 '16 at 10:08

1 Answers1

1

So what the error means is that you should use a UIAlertController instead.

Usage is quite straightforward:

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)

Where action1 has a system style .Cancel and no handler, and action2 has a default style and a handler.

Changing the color of the actions can be done using the tintColor attribute of the views, see this answer.

Community
  • 1
  • 1
xoudini
  • 7,001
  • 5
  • 23
  • 37
  • Thanks! Which will be the action that let me change the message from `"\(tabla)"` to `"\(completo)"`? – Javi Rando Feb 05 '16 at 10:11
  • Just create one `UIAlertAction` for each, and handle what each action should do on the line underneath `(action) in`. And always remember to add the actions to your `UIAlertController` – xoudini Feb 05 '16 at 10:12
  • I am sorry but I am very new with Swift. When I create an action, a new alert opens and the previous buttons disappear. How can I program just to change the message? – Javi Rando Feb 05 '16 at 10:22
  • So you want to change the message on the alert when pressing an action, and never dismiss it? – xoudini Feb 05 '16 at 10:24
  • One of the action will dismiss it and the other will change the message – Javi Rando Feb 05 '16 at 10:24
  • In that case it might be best for you to create a completely custom view that imitates the `UIAlertController`, I don't think there's a way to not dismiss the alert on an action. – xoudini Feb 05 '16 at 10:34