I'm using multiple segues in Swift and I have a UIAlertView
in a table view. The UIAlertView
shows perfectly. When I click the first option (Ver Mapa), it changes perfectly to another view controller (Mapa) with segue (go_to_mapa
). But when I press the second option (Ver detalle) with segue (go_to_detalle
) to change to the other view controller, it doesn't work. It doesn't do anything. How can I resolve this problem?
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
var refreshAlert = UIAlertController(title: "Menu", message: "Seleccione una opcion", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ver Mapa", style: .Default, handler: { (action: UIAlertAction!) in
if (segue.identifier == "go_to_mapa") {
var svc = segue!.destinationViewController as Mapa;
svc.cuenta = self.cuenta
svc.user = self.user
svc.password = self.password
let indexPath = self.tableView.indexPathForSelectedRow();
let currentCell = self.tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
self.navigationController?.pushViewController(svc, animated: false)
}
}))
refreshAlert.addAction(UIAlertAction(title: "Ver Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
if (segue.identifier == "go_to_detalle") {
let vc = segue.destinationViewController as Detalle_Vehiculo
}
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
prepareForSegue:
calls shorty before presenting new controller. From the doc:
Notifies the view controller that a segue is about to be performed.
So you shouldn't show any alerts, pop-ups whatever in that method. Instead, you should take destinationViewController
and adjust it properly before presenting.
Instead, you should show alerts in tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
like the following:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var refreshAlert = UIAlertController(title: "Menu", message: "Seleccione una opcion", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ver Mapa", style: .Default, handler: { (action: UIAlertAction!) in
self.performSegueWithIdentifier("go_to_mapa", sender:self)
}))
refreshAlert.addAction(UIAlertAction(title: "Ver Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
self.performSegueWithIdentifier("go_to_detalle", sender:self)
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
And prepare it:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "go_to_mapa" {
var svc = segue!.destinationViewController as Mapa
svc.cuenta = self.cuenta
svc.user = self.user
svc.password = self.password
let indexPath = self.tableView.indexPathForSelectedRow();
let currentCell = self.tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
}
else if segue.identifier == "go_to_detalle" {
let vc = segue.destinationViewController as Detalle_Vehiculo
}
}