Reputation: 587
I have data being shared by two tabs of a UITabBarController
. The first tab is in charge of generating and updating the array of data which is consumed by the second tab.
My original method of sharing the data between the tabs worked.
------UIViewController
---UITabBarController
------UIViewController
Using the above method, I accessed the array of data from the first tab as such:
let pointsViewController = self.tabBarController?.viewControllers?.first as! ShowPointsViewController
tableHeaders = pointsViewController.tableHeaders
With ShowPointsViewController being the the first tab
Here is my issue:
When I embed my first UIViewController
into a UINavigationController
and then try to access my array of data using code below, I get a runtime error because the code after the .first...
is returning as nil. I understand I am accessing this incorrectly, what is the correct way of achieving the same result?
let pointsViewController = self.tabBarController?.viewControllers?.first?.navigationController?.topViewController as! ShowPointsViewController
Upvotes: 0
Views: 571
Reputation: 770
If I understand well you created something like this:
------UINavigationController ------UIViewController
---UITabBarController
------UINavigationController ------UIViewController
You are getting nil as self.tabBarController?.viewControllers?.first is already UINavigationController.
Use following code:
let pointsViewController = (self.tabBarController?.viewControllers?.first as? UINavigationController)?.topViewController as! ShowPointsViewController
Upvotes: 3
Reputation: 19602
self.tabBarController?.viewControllers?.first?
is your navigationController
, so you are calling .navigationController
on the navigationcontroller, which returns nil
.
To fix this, remove .navigationController
from your call:
let pointsViewController = self.tabBarController?.viewControllers?.first?.topViewController as! ShowPointsViewController
Upvotes: 0