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 ?
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
Related
I had a tabbarcontroller application in which i am created that like this
UITabBarcontroller (UIWindow's rootViewController)
->UINavigationController (first tab)
-->UIViewController
->UINavigationController (second tab)
-->UIViewController
in the navigation bar i had a button in which i am pushing another view controller to the self .navigation controller.But now i want the same action from the other navigation controller also. so i need to push to the navigation controller which is the top view controller.I need to replace the [self.navigationController pushViewController:viewcontroller animated:NO]; with in a way that whichever is the navigation controller present there in the tabbar controller we need to add to that.Can any body help me on this?
Well how simpler than holding a refference to the UInavigationController that is currently visible (you should hold that refference from the UITabBarController delegate tabBarController:didSelectViewController:)
So you add in your AppDelegate a variable:
UINavigationController *navController;
then :
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController.navigationController)
navController = viewController.navigationController;
}
Now, you do the push this way:
if (navController)
[navController pushViewController:viewcontroller animated:NO];
So at first you need have access to TabBarController. I think you can get it somewhere in AppDelegate.
then you can just call:
UIViewController *ctrl = myTabBarCtrl.selectedViewController;
[ctrl.navigationController pushViewController:viewcontroller animated:NO];
Let me explain clearly.
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 a tab using below,
third = [[Third alloc] initWithNibName:#"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];
If I switch to next tab and come back to the previous tab, it should popto the previous view controller, how to do this?
I tried
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
not succeed,
I also tried,
[third popto];
In the third viewcontroller,
-(void)popto
{
[self.navigationController popViewControllerAnimated:YES];
}
nothing happened.
Now, I have to click the tab again to poptoviewcontroller to the first viewcontroller.
Any ideas will be highly appreciated.
You should try using
[viewController.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
instead of
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
Try the below code snippet
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)viewController;
if (navController && ([[navController viewControllers] count] > 0))
{
[navController popToRootViewControllerAnimated:NO];
}
return YES;
}
Hope it may work for you.
How about overriding UITabbarController for a custom one and implement the following method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
Next loop trough all the 'viewcontrollers' of the tabbar (in this case the navigation controllers) and call on all the navigation controllers popToRootViewController.
This might work.
In my iPhone application I am using UITabbarController + UINavigationController, created in appDelegate.
My issue is as follows:
On screen- I have tableview. When one selects cell I am pushing the controller to screen-B. Now without poping it if I select another tab from tabbar, then view gets refreshed but navigationbar is not getting refreshed. It displays navigationbar of screen-Bscreen-B.
Used below code but nothing seems to solve my issue:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]])
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
}
try this in appdelegate
UINavigationController *screen1=[[UINavigationController alloc] initWithRootViewController:controller1];
UINavigationController *Bscreen=[[UINavigationController alloc] initWithRootViewController:controller2];
NSArray *tabbararray=[[NSArray alloc] initWithObjects: screen1,Bscreen,nil];
self.TabControllerObj.viewControllers=allviewcontrollers;
and make tabbar as root viewcontroller of your window
I want navigate to Rootview of 1st Tabbar item by clicking a button on fourth tabbar using this code just changes the Tabbar selection
code snippets
[self.parentViewController.tabBarController setSelectedIndex:0];
Previous action to be appear in the home view controller .
i need Direct navigation of Home view controller in main page
How to resolve in this issue?
Thanks in advance
First get your UINavigationController of the first tabbar item.
UINavigationController *navController = [self.tabBarController.viewControllers objectAtIndex:0];
And then navigate to root view controller.
navController popToRootViewControllerAnimated:NO];
That's all. :)
You have to pop the selected tab bar navigation stack to root.. You can achieve this by several method one is as below..
In your AppDelegate implement the tabbarcontroller delegate function, make sure you have set the tabbarcontroller delegate to AppDelegate..
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//Check the selected index to 0
if ([viewController isKindOfClass:[UINavigationController class]] && tabBarController.selectedIndex == 0) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
When I receive a Push Notification, I want to jump to the middle tab on the tabbar and reset the tab (it has multiple table views that the user may have navigated into). I am able to jump to the tab using tabBarController.selectedIndex = 1; ... but how do I reset to the first view?
Implement UITabBarControllerDelegate on a controller class (perhaps your app delegate) and implement tabBarController:didSelectViewController: to pop all pushed view controllers off the navigation stack:
- (void) tabBarController:(UITabBarController *) tabBarController didSelectViewController:(UIViewController *) viewController {
if([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
}
}