hacker
hacker

Reputation: 8947

How to access the particular viewcontroller from the tabbarcontroller?

I had an application in which i had a tabbarcontroller adding to the window of the application.In the tabbarcontroller i had two tab bar items each having navigation controller,in which corresponding view controllers are connected.Now i need to access an instance variable of the particular view-controller which is inside the navigation controller in the tab bar controller.I am doing like this but of no use:

NSArray *mycontrollers = self.tabBarController.viewControllers;
NSLog(@"%@",mycontrollers);
self.secondviewcontroller=(SecondViewController *)[mycontrollers objectAtIndex:1];
self.secondviewcontroller.var=self.var;

But it has thrown some error message as

-[UINavigationController setvar:]: unrecognized selector sent to instance can anybody help me figuring out how to achive the particular view controller from the hirarchy of this navigationcontrollers.

Upvotes: 1

Views: 95

Answers (1)

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Try:

NSArray *mycontrollers = self.tabBarController.viewControllers;
NSLog(@"%@",mycontrollers);
UINavigationController *nvc = [mycontrollers objectAtIndex:1];
self.secondviewcontroller=(SecondViewController *)[nvc topViewController];
self.secondviewcontroller.var=self.var;

The reason your app was crashing because self.tabBarController.viewControllers; was returning the navigation controllers.

Upvotes: 1

Related Questions