Swift iOS prepareForSegue

4

I'm just starting with Swift and I have a doubt. I have a controlador_uno : UIViewController and another controlador_dos : UITableViewController .

The thing is that I send a varible by segue from controlador_uno to two and in controlador_dos it does not take it.

driver_one: UIViewController

override func prepareForSegue(segue: UIStoreboardSegue, sender: AnyObject?)
 {
  if segue.identifier == "SendDataSearch"  
  {
    if let destino = segue.destinationViewController as ? Controlador_dos
    {
       destino.varSegue = txtNom.text
    }
  }
}

driver_two: UIUITableViewController {

var varSegue = ""

...
}

Can you pass variables from UIViewController to UITableViewController ? I've done things from tableviewcontroller to viewController and they work.

    
asked by fabian 08.02.2016 в 05:17
source

3 answers

2

The first thing is to make sure that the name of the segue is well written, you can put a breakpoint in the init of the controller_dos and you will see if it really gets there. Once you make sure you arrive. I would define varsegue in this way, without assigning value to it:

var varSegue: String?

EL prepareForSegue only goes in one direction.

    
answered by 02.04.2016 в 13:16
1

It is very likely that the problem comes because segue.identifier comes empty, and when you do the if to check that it is SendDataSearch and that the Controlador_Dos exists does not get to enter the first if . Therefore, verify that you have correctly set the identifier in the following way:

    
answered by 08.02.2016 в 12:21
0

First of all I would recommend you give the code a better shape:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "SendDataSearch"  {
        let destino = segue.destinationViewController as ? Controlador_dos

        destino!.varSegue = txtNom.text  
    }
}

I have a doubt, when you refer to txtNom.text , what data do you mean? This could be a failure.

Another is as you already commented before the fact of not having given an identifier to the segue in the storyboard.

    
answered by 22.02.2016 в 14:58