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];
}
}
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];
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
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
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];
}
}
How to reset a uinavigationview to display the root controller when user clicks back to it in a tab bar app
Hey,
Just wondering how I would do this. I have the navcontroller in my delegate along with the tabbar controller and Any time the user clicks to another tab I want the rootview on the navigation controller to be shown if and when they click back the the tab that contains the uinavcontroller.
Does this make sense?
Nick
[self.navigationController popToRootViewControllerAnimated:YES];
Or NO if you don't want it to animate.
This way all the views that were cached are still there, i.e. you don't "remove/release" all the views above the root view, unless the navigationController deems it necessary.
I hope this was what You were looking for..
place the code in appdelegate.m
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)viewController;
[nav popToRootViewControllerAnimated:NO];
}
When using the UITabBar delegate method, you must delay the popToRootViewControllerAnimated call.
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *view=(UINavigationController *)self.selectedViewController;
[view performSelector:#selector(popToRootViewControllerAnimated:) withObject:nil afterDelay:.5];
}
}