Swift : Want to send data from UITabBarController to NavigationController

I am Swift beginner.
Don’t know how to send data from UITabBarController to NavigationController→UITableViewController
I confirmed data sent from UITabBarController to UIViewController with code 1 below.

(Code 1)
if let chartVC = self.viewControllers?[1] as? ChartViewController{
chartVC.stepTarget.text = "Target : " + data
}

Can anyone suggest how to write the code in case NavigationController is in place?

In another way, I confirmed data sent to newsVC instance with code 2 below.
But after I go to NewsViewController tab, data was reset.
Don’t know why.

(Code 2)
if let newsVC = self.storyboard?.instantiateViewController(identifier: “newsVC”) as? NewsViewController {
newsVC.rssArray = data
print(newsVC.rssArray)
self.navigationController?.pushViewController(newsVC, animated: true)
}

Can anyone suggest how to fix ?
Thank you very much !

BR
Tada Terao

You don’t necessarily need to use prepareForSegue for this. Just reference which ViewController in the TabBarController viewControllers array that you want and cast it.

let vc = self.tabBarController.viewControllers![1] as! HomeViewController
vc.templateForCell = templates

If the ViewControllers in your TabBar are embedded in Navigation Controllers, you can do this:

let navController = self.tabBarController.viewControllers![1] as! UINavigationController
let vc = navController.topViewController as! HomeViewController
vc.templateForCell = templates

That’s Work.

This topic was automatically closed after 166 days. New replies are no longer allowed.