Pass tableview value to tabbar

Asked

Viewed 37 times

0

Hello,

I’m developing my first app and I’m having a question.

I have a tableview, and I would like her indexpath.Row to be visible in the 2 viewcontroller of a tabbar, to click on a label. I tried to do it the way below, but it didn’t 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]

    }

}

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 directly to the screen objects as they are instantiated after loading, at this point they do not yet exist.

Create variables in the target view, and pass the values to them. Ai in the method viewDidLoad from 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
}
  • Thanks Celso, I’ll test later and tell you if it worked! Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.