UITabBarController: pop to root while select another tab just like in iPod App
hi All,
in iPod application, click on "playlist" tab , then play list table will display, and click any playlist(cell) then it will drill drown to another screen
click on other tab (say "Album" tab) and click again "playlist" tab then the "playlist" tab will display the playlist instead of already drilldown screen
how can we achieve the same.
It will be great if we can pop to root of "playlist" tab while click on other tab(say "Album").. In this way we can reduce the memory using in run time..
i.e requirement is : when click on a tab, then pop to the root view of last selected tab
i am also looking for a way to do this and it's driving me crazy. all of the sample apps for UITabBarController don't do this; but when i show people my app, they get really confused when they re-click on a tab and it's stuck somewhere besides the top.
The approach I am using in my own program is to only show the tab bar while a root view controller is on the screen. As soon as the user navigates deeper, I use this:
[self setHidesBottomBarWhenPushed: YES];
...to hide the tab bar. Therefore the user cannot switch tabs when I don't want them to, and they much navigate their own way back to the top before switching tabs. This also makes more screen real estate available deeper in the program.
The approach I am using in my own program is to only show the tab bar while a root view controller is on the screen. As soon as the user navigates deeper, I use this:
[self setHidesBottomBarWhenPushed: YES];
...to hide the tab bar. Therefore the user cannot switch tabs when I don't want them to, and they much navigate their own way back to the top before switching tabs. This also makes more screen real estate available deeper in the program.
brianslick - the popToRoot did the trick. i had a few problems that were code-related but now it works. thanks...
set up the tab controller (viewControllers is an array of navigation controllers) and set the delegate as self (the AppDelegate, where this code resides)
Code:
tabBarControllerMain = [[UITabBarController alloc] init];
tabBarControllerMain.viewControllers = viewControllers;
[tabBarControllerMain setDelegate:self];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarControllerMain.view];
i have a hierarchy of 3 layers; if i am in the 3rd layer (a product detail page), click on a different tab, and then come back to the previous tab - the hierarchy is popped (and animated as such.)
- chuck
Last edited by carendt242; 08-03-2009 at 12:17 AM.
Are you sure that you need the "NSArray *a =" part? I don't think that is doing anything. For one thing, I don't think the pop command has any kind of return value.
Are you sure that you need the "NSArray *a =" part? I don't think that is doing anything. For one thing, I don't think the pop command has any kind of return value.
popToRootViewControllerAnimated returns an array of the view controllers you popped... there might be a more elegant way to deal w/ this but i'm just in the 'get it working' stage right now.
ok.. [viewController popToRootViewControllerAnimated:YES]; is enough..
Actually I thought we can do this by click on another Tab. (bcoz currently we are doing it when click on same Tab again.. And user can do this same when just click again the Tab) ok leave it
popToRootViewControllerAnimated returns an array of the view controllers you popped... there might be a more elegant way to deal w/ this but i'm just in the 'get it working' stage right now.
- chuck
Ah, you are correct. I should have looked at the docs before I spoke. I only do this in maybe one spot, and I wasn't doing any assignment.
And i got it done. I am getting the exact output what i need. But there is a warning. How to clear the warning... Plz help.
Code:
29: warning: 'UIViewController' may not respond to '-popToRootViewControllerAnimated:'
29: warning: (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
And i got it done. I am getting the exact output what i need. But there is a warning. How to clear the warning... Plz help.
Code:
29: warning: 'UIViewController' may not respond to '-popToRootViewControllerAnimated:'
29: warning: (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
I get the same warning. Any suggestions out there?
but without success. This method shows nothing in the console, so I'm obviously not calling it correctly.
In my AppDelegate in applicationDidFinishLaunching is also my TabBarController which holds an array of 5 Navigation Controllers for each Tab Bar item. Everything is working fine with the tab bars.
Thanks again tho for pointing out sending the message to a Navigation Controller. I've spent more time searching for a solution, but there's not a lot of documentation at Apple on didSelectViewController.
Well, not just any navigation controller, it has to be the navigation controller that is inside the desired tab.
If you are within a view controller already, they way to pop is:
Code:
[[self navigationController] pop...];
However, you are not within a view controller already - or at least not the right one - so 'self' won't work. So you'll have to find a view controller in the desired tab, and use it instead of self. See the UITabBarController documentation for some possible methods to identify a view controller. Then the above code should change to:
I'm not certain if the animation will work properly, since it may have to display 2 different things in order to accomplish the goal. If it doesn't work right, might consider using tabBarController:shouldSelectViewController: instead.
Thanks for all your help Brian. I haven't gotten to work with my Tab Bars, but I've gotten it to work in other areas where there were navigation controllers. I'm going to see if there's a way for me redo the app delegate to bring so the tab bars pop back to the root view.
Brian, your suggestion worked, I had forgotten to thank you.
I rebuilt the way my Tab Bar is set up in my AppDelegate, set up 5 Navigation Controllers (one for each view controller) in the MainWindow nib, and added this code:
Can you tell me why you added this method with no code?
I got the 'poptoroot' piece working now, with help from this thread, but I'm wondering what this method does.
Seems to work okay without it.
What if there's no UITabBarAppDelegate? In mine, the RootViewController is a UIViewController that happens to have a UITabBarController as a class a variable. As such, this happens:
What if there's no UITabBarAppDelegate? In mine, the RootViewController is a UIViewController that happens to have a UITabBarController as a class a variable. As such, this happens:
How would I override the AppDelegate methods for this instance variable UITabBarController?
A. You're most likely doing it wrong if you have a tab bar controller inside another view controller.
B. Anything can be a delegate, it just needs to implement any required delegate methods. It can be your root controller here. You just have to tell the tab bar controller who the delegate is.