I have four UINavigationControllers assigned each to a tab in a UITabBarController. Each UINavigationController manages a UIViewController, which may itself branch out into other UIViewControllers below it hierarchally.
My question is, in a case in which a user, under one tab, has navigated to a UIViewController that hierarchally BELOW the main UIViewController managed by the UINavigationController, and then the user pushes a different tab, and then goes back to the original tab, HOW can I make it so that the user is presented with the main UIViewController managed by the UINavigation controller? and not the page where he left off?
UITabBarController is set up in IB
Implement the UITabBarControllerDelegate protocol and pop to the root controller whenever your delegate is notified that the user has selected a different tab.
Something like:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[viewController.navigationController popToRootViewControllerAnimated:YES];
}
The code above should pop to the root controller of any navigation controller that is the main view controller of the newly selected tab. You could try implementing the protocol in your application delegate. And do not forget to actually assign the app delegate as the tab bar controller's delegate.
didn't work for me originally, then later on I noticed that all my viecontrollers for different tabs are actually UINavigationControllers
and thus I modified the above code a little bit as follows and it worked:
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
Related
I'm building an app that combines tabbar and navigation bar. In one of tabs, I have a UIButton and by touching it, I push a Tableview into stack of navigationcontroller. Then, without returning to the root view controller manually by pressing Back buttons I change to another tab from tabbar and when I come back to the tab with the tableview inside, I get bad access error.
I've already tried popping the tableview from navigation controllers stack, or not releasing the tableview but I could't make it.
Thanks...
Assuming you've created your tabBar and Navigation Controllers in the AppDelegate, you can code your app to return each tab to the root view when it is selected.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (viewController == firstViewNavigationController) {
[firstViewNavigationController popToRootViewControllerAnimated:NO];
} else if (viewController == secondViewNavigationController) {
[secondViewNavigationController popToRootViewControllerAnimated:NO];
}
}
I am using this and it is working fine in my app. I am releasing my Navigation Controllers in the dealloc method. I have also made my AppDelegate conform to the UITabBarDelegate protocol.
I have made an app in which i have 2 tabs.and there are navigation controller in them,
The issue is that once i click on the tab bar and there is navigation bar and the view changes as i drill down.But when i go to the second tab and then come back to the 1st tab then it opens the view which was there previously when i changed the tab.So it retain the view whereas i want that that tab should start up with the same first initial view.
From where we can do this by info.plist or what..>?
Thanks in advance ...
Adopt UITabBarControllerDelegate protocol and implement tabBarController:shouldSelectViewController: method.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)controller {
[(UINavigationController *)controller popToRootViewControllerAnimated:NO];
return YES;
}
This assumes that all tabs have navigation controllers.
That's not how tab view controllers work. You can implement this method in your app delegate (after making it the delegate for the UITabeBarController)....
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Then call a reset method (or similar) on your view controller to pop back to the root view controller.
This is not how you normally work with UITabBarControllers however....
You can implement
- tabBarController:didSelectViewController:
in the delegate of the UITabBarController and then call
– popToRootViewControllerAnimated:
on the UINavigationController
I have a UITabBarController setup with 2 UINavigationControllers.
One UINavigationController has One UIViewController, the other UINavigationController has Two UIViewControllers. If you then navigate to the second UIViewController and click the Tab that is already selected it bring you to the root of the UINavigationController (This would be the first UIViewController).
Is there a way to stop this from happening? I do not want the user to be able to click an already selected Tab to go to the root of the Navigation Controller.
To do this you need to implement a function in your app delegate to pick up the tabbar delegate calls.
In your app delegate.m file, in the didfinishlaunching method, add this line
[tabBarController setDelegate:self];
then implement this method (also in your app delegate):
- (BOOL)tabBarController:(UITabBarController *)theTabBarController shouldSelectViewController:(UIViewController *)viewController
{
return (theTabBarController.selectedViewController != viewController);
}
This gets called as part of the tab delegate protocol and will stop the selection of a tab if its already the selected one.
Hope that helps.
I have a tabbar app. each tab has uinavigationcontroller. but all navigationcontroller's Shows Navigation Bar value is NO.
i control the which navgation control is active in
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
event in AppDelegate. Then i the user taps a row in a table in a tab i call the same method (openDetail)in appdelegate. i want to push DetailViewController full screen, not in tabs controller. i tried some ways but never worked. so i push it as a modalview.
[currentNavController presentModalViewController:detailVC animated:YES];
how can it come/go from right side.
i just want it to work like normal rootviewcontroller. but new controller should come over tabbarcontroller like USA Today app.
present the view controller like this
[tabBarController presentModalViewController:detailVC animated:YES];
I have the next question:
In my project I have the next:
UItabbarController
....Some UINAvigationControllers....
*(1) UINavigationController
UIViewController (UItableView) - When select one row it goes to...(by push)
UIViewController (UItableView)
My problem is when i click in the tab bar item, I see the viewController view like last time that i saw this, and no reload to the *(1) first view another time.
Where I need to write sth for each time that i click in a tab bar item i reload the first view of this tab bar item.
Thanks!
If I understand your question correctly, you are trying to return the navigation controller to the root element when it's tab bar item is selected.
To do this, set some object (e.g. your application delegate, but it can be some other object instead) to be the delegate for your UITabBarController. (If you use the application delegate, it will be the delegate for more than one thing, which is fine.) Then, implement the tabBarController:didSelectViewController: method. In that method, tell the selected view controller (which should be a NavigationController) to return to the root view controller.
Something like this. Add this implementation to your AppDelegate.m class:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[viewController popToRootViewControllerAnimated:NO];
}
In your .xib file, set the delegate for the TabBarController to the the AppDelegate. (If you are programmatically creating the tabBar, you'll have to do it programmatically there.)
As you suspected, trying to do this in viewWillAppear method, or anything other method, of the view controller that live in the navigation controller is not the correct approach. It is a method to perform on the navigation controller, and detected by the delegate of the tab bar.
Try putting your callback code to reload the view in the viewWillAppear or viewDidAppear methods. These are both called every time a view controller displays it's view on the screen.
Also feel free to copy and paste your actual code, it usually makes things easier on our end :)