Reputation: 7333
I have navigation controller on tab bar item 2 . I am doing some operations in tab bar item 2->navigation item 2 . when I click on button I want to go to the tab bar item 1 but at the same time I want navigation controller in 2nd tab to be set to its root view controller . I have tried following :
[self.tabBarController setSelectedIndex:0];
[self.navigationController popToRootViewControllerAnimated:NO];
But when I came back from tab 1 to tab 2 . It is giving me exception :"message sent to deallocated instance" . What is the correct way to achieve this ?
Upvotes: 0
Views: 221
Reputation: 4561
To select first tab:
[self.tabBarController setSelectedIndex:0];
To switch tab's rootviewcontroller:
To achieve this you have use UITabBarBontroller's
delegate method to pop to rootviewcontroller.
write it in AppDelegate
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex==1) {
//only for tab number 2
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)viewController ;
[navController popToRootViewControllerAnimated:NO];
}
}
}
P.S. don't forget to add UITabBarControllerDelegate
in AppDelegate.h
Upvotes: 1