-1

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)
}   
TylerP
  • 9,600
  • 4
  • 39
  • 43

1 Answers1

0

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
    }
}
Azat
  • 6,745
  • 5
  • 31
  • 48
  • It shows the error at the map viewController, cause i pass some data to the map view controller – Joel Felix ST Joy May 09 '15 at 16:48
  • How can this occur if you removed all code that actually navigates to any `otherviewcontroller`? Please make sure that you do not have neither `tableView:didSelectRowAtIndexPath:` nor `prepareForSegue:sender:` implemented and nothing happens when you click at table row. If something still happens, please remove all that code too – Azat May 09 '15 at 16:51
  • But when i used my code that i shown above, the alert box shows perfectly in the tableview. When i click "ver Mapa", works perfectly. The problem is when i click "Ver vehiculos". – Joel Felix ST Joy May 09 '15 at 16:55
  • Don't use your posted code anymore. From the state of your sources when nothing happens at table click, please again copy and paste all my code from the answer to your project as is. It should work as you need – Azat May 09 '15 at 16:57
  • I updated it several times after first post, maybe you have copied one of the earlier version – Azat May 09 '15 at 17:01
  • What do you mean? Do you able to see at least alert? – Azat May 09 '15 at 17:24
  • No bro, the alert box still shows at the map view controller – Joel Felix ST Joy May 09 '15 at 17:27
  • Ok, replace all `self.performSegueWithIdentifier` in `tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath` with some `println` statements. Do it still navigates to map? – Azat May 09 '15 at 17:29
  • Yes it still navigates to map – Joel Felix ST Joy May 11 '15 at 16:35
  • I asked you to remove __ALL__ code that you previously had. I want you to make the state of your code that __NOTHING__ happens when you click elsewhere in the table. And now you reply that you still have code that moves you to the map. Please read again all our conversation – Azat May 11 '15 at 16:38
  • Bro, i removed all code that i previously had, and it still does the same thing – Joel Felix ST Joy May 11 '15 at 18:53
  • I also suggested you to look at the storyboard for actions from cell, because this is the only possible way why you still getting map controller to show – Azat May 11 '15 at 18:55
  • At the storyboard, i have two segues from the tableview. One of them says : (Push segue to Detalle_Vehiculo) and the other says : (Push segue from Cell to Mapa) – Joel Felix ST Joy May 11 '15 at 20:09
  • Please remove both of them – Azat May 11 '15 at 20:11
  • When i click "Ver mapa" it shows me this error : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'go_to_mapa'' – Joel Felix ST Joy May 11 '15 at 20:14
  • and when i click "Ver detalle" it shows me: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'go_to_detalle'' – Joel Felix ST Joy May 11 '15 at 20:15
  • That is much better! Now return back to your storyboard and reconnect segues, but drag them not from table cell, but from whole view controller. Please refer [here](http://stackoverflow.com/a/8234454/1565335) to do it right as I ask you – Azat May 11 '15 at 20:25
  • It works bro, and thank's so much. i already voted your answer. Thank's a lot. – Joel Felix ST Joy May 11 '15 at 20:40
  • You are welcome! This was one of the most difficult answers at SO:) Good luck! – Azat May 11 '15 at 20:43