Reputation: 8444
Let me explain clearly now,
I have a tabbarcontoller in the viewcontroller which is the main view controller of the single view application project.
I added a tabbarcontroller to the viewcontroller as the subview. In the tabbarcontroller, I added two navigation controllers like below image,
I have added three(named First, Second, Third) more viewcontrollers as new file.
If I navigate from one viewcontroller to other in the first tab using below code,
third = [[Third alloc] initWithNibName:@"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];
Then I switch to second tab by clicking the tab below the tabbarcontroller.
and then
If I switch to next tab(first tab) by clicking a button in third view controller using,
-(IBAction)switchtab
{
vc.tabctrl.selectedIndex=0;
//vc is the main viewcontroller to which the tabbarcontoller(tabctrl) added as subview
}
After switching to next tab, there I should poptorootviewcontroller, I tried below code
-(IBAction)switchtab
{
vc.tabctrl.selectedIndex=0;
[vc.tabctrl.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
But it jumps to next tab but popToViewController not working, any suggestions?
Upvotes: 1
Views: 666
Reputation: 12389
I did not understand the whole structure here, but I have realised that you are using different controllers to push the vc "third" and to pop to root. Can you try:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
in the same class you are using to push the "third"?
Edit: Another option, can you try this:
-(IBAction)switchtab
{
vc.tabctrl.selectedIndex=0;
[(UINavigationController*)vc.tabctrl.selectedViewController popToRootViewControllerAnimated:YES];
}
Upvotes: 2
Reputation: 5268
If your viewcontroller are of navigation controller then following code will change to root view when you switch the tab
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController*)self.tabBarController.selectedViewController popToRootViewControllerAnimated:YES];
}
}
Upvotes: 3