screenMonkey MonkeyMan
screenMonkey MonkeyMan

Reputation: 423

How do I instantiate navigationController while passing data to viewController?

So I have a tabBar. Clicking a certain tab will take the user to a viewController which is embedded in a navigationController. In order to have the navigationController included, I instantiate the viewController using

myViewController = [storyboard instantiateInitialViewController];

rather than

myViewController = [storyboard instantiateViewControllerWithIdentifier:@"myID"];

The later code pushes the viewController but without the navigationController

This is fine up until the point I want to pass data to the instance myViewController. Thing is, I can't pass data with the instance referring to the initial viewController (which is the navigationController), but I can do it using an instance referring directly to myViewController. In other words:

This works in order to get data (but no navigationController):

viewController = [storyboard instantiateViewControllerWithIdentifier:@"experienceID"];
((ExperiencesListViewController*)viewController).experiences = self.experiences;

and this crashes if I try to add data, but gives me a navigationBar if I exclude setting data:

viewController = [storyboard instantiateInitialViewController];
((ExperiencesListViewController*)viewController).experiences = self.experiences;

Hope I explain this well enough. Let me know if there is something I need to clarify.

EDIT My first thought was using prepareSegue but that doesn't seem to trigger when moving between the tabs. Another thought is to access myViewController through the instance in some way, but not sure how.

Upvotes: 1

Views: 196

Answers (1)

Guilherme Matuella
Guilherme Matuella

Reputation: 2273

That's because the initial UIViewController of that particular storyboard is a UINavigationController.

A simple way to get the VC that you want is getting such UINavigationController by calling:

navController = [storyboard instantiateInitialViewController];

just like you've done, and then:

navController.childViewControllers[0]

This will return the first VC of that particular navigation controller (assuming, of course, that it contains solely the VC that it's embedded in), which is probably your ExperiencesListViewController

Upvotes: 1

Related Questions