简体   繁体   中英

Pass data from tableview to view controller in tab bar in Swift

Im fairly new to Xcode and seems to be having a bit of a problem. I was passing data from my tableviewcontroller to EventViewController and it worked fine with the code bellow:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if let destination = segue.destination as? EventViewController {
    destination.Event = events[(tableView.indexPathForSelectedRow?.row)!]
  }
}      

But I needed to make some changes to the app and Embedded a tab bar controller to the EventViewController, and now I can't get it to run. How do I pass data to the First tab in the Tabbarcontroller instead? :) Thank you very much for your help.

Try this

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = (segue.destination as? UITabBarController)?.viewControllers.first as? EventViewController {

        destination.event = events[(tableView.indexPathForSelectedRow?.row)!]

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM