Pass value from tableview to tabbar

0

Hello,

I'm developing my first app and I have a question.

I have a tableview, and I would like the indexpath.row to be visible in the 2 viewcontroller of a tabbar, to load a label. I tried to do it as below but it did not work.

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

    if(segue.identifier == "detail"){
        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
        let tabVC = segue.destinationViewController as! UITabBarController
        let Detalhe1 = tabVC.viewControllers![0] as! Detalhe1ViewController
        let Detalhe2 = tabVC.viewControllers![1] as! Detalhe2ViewController

        //Labels da primeira viewcontroller
        Detalhe1.ID.text = arr1[indexPath.row]
        //Labels da segunda viewcontroller
        Detalhe2.ID.text = arr2[indexPath.row]

    }

}
    
asked by anonymous 28.07.2016 / 16:19

1 answer

1

The problem seems to be here:

//Labels da primeira viewcontroller
Detalhe1.ID.text = arr1[indexPath.row]
//Labels da segunda viewcontroller
Detalhe2.ID.text = arr2[indexPath.row]

You should not pass the values straight to the screen objects, as they are instantiated after loading, at this point they do not yet exist.

Create variables in the Destination View, and pass the values to them. Then in the% method of% of the view that will be shown, pass the values to the screen components.

// Table view
tabvc.detalhe1 = arr1[indexPath.row]
tabvc.detalhe2 = arr2[indexPath.row]

// View de destino
var detalhe1 : String?
var detalhe2 : String?

override func viewDidLoad() {
    super.viewDidLoad()

    Detalhe1.ID.text = detalhe1
    Detalhe2.ID.text = detalhe2

    self.view.backgroundColor = backgroundColor
}
    
28.07.2016 / 16:56