Joel Felix ST Joy
Joel Felix ST Joy

Reputation: 39

prepareForSegue doesn't work in swift. How can i resolve it?

I have two viewcontrollers with custom 'push' Segue in swift. When i select a row in tableview it shows me perfectly an alert box with three options. My problem is when i select the first option (Ver Mapa) to change to the anotherviewcontroller, it doesn't work. Does not do nothing. How can i resolve it please?

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;
            self.performSegueWithIdentifier("go_to_mapa", sender: self)
            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!


        }



    }))
    refreshAlert.addAction(UIAlertAction(title: "Detalle Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
        println("Detalle Vehiculo")
    }))
    refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
        println("Ejecutar comandos")
    }))

    refreshAlert.addAction(UIAlertAction(title: "Cancelar", style: .Default, handler: { (action: UIAlertAction!) in
        self.back()
    }))

    presentViewController(refreshAlert, animated: true, completion: nil)

}

Upvotes: 0

Views: 753

Answers (3)

Joel Felix ST Joy
Joel Felix ST Joy

Reputation: 39

I resolved it with no animate: false like this:

...
...
svc.Device = currentCell.detailTextLabel!.text!
            svc.desc = currentCell.textLabel!.text!

            self.navigationController?.pushViewController(svc, animated: false)

Upvotes: 0

Mundi
Mundi

Reputation: 80265

In the action handler you have to call the segue, not check it. You check the segue in prepareForSegue...

// calling the segue
self.performSegueWithIdentifier("go_to_mapa")

Upvotes: 0

Duncan C
Duncan C

Reputation: 131418

Your code is messed up.

The code to display your alert does not belong in prepareForSegue. You should not do any UI in prepareForSegue. You also should not call performSegueWithIdentifier inside prepareForSegue. The system calls your prepareForSegue method after you or the user have triggered a segue, before the new view controller is displayed. It gives you a chance to set things up.

The code you have posted is unlikely to be showing an alert.

Here's what you want to do:

If you want to trigger an alert on the user selecting a cell in your table view, implement the table view delegate method didSelectRowAtIndexPath:. In that method, create and display your alert controller. When the user chooses an option, THEN you should call performSegueWithIdentifier if you want to trigger a segue.

Once you've triggered a segue the system will call your prepareForSegue method

Upvotes: 1

Related Questions