Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-24-2009, 12:00 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default 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



using iPhone OS 3.0, sdk 3.0
david_david is offline   Reply With Quote
Old 07-29-2009, 04:08 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 7
carendt242 is on a distinguished road
Default

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.
carendt242 is offline   Reply With Quote
Old 07-29-2009, 08:55 AM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

To answer the specific question, I believe you will need to employ these two methods:

UINavigationController: -popToRootViewControllerAnimated:
UITabBarControllerDelegate: tabBarController:didSelectViewController:

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-29-2009, 11:22 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default

yes.. me too was using the same now..
but it will be good if we can simulate the same like in iPod..
david_david is offline   Reply With Quote
Old 08-02-2009, 08:57 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 7
carendt242 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
To answer the specific question, I believe you will need to employ these two methods:

UINavigationController: -popToRootViewControllerAnimated:
UITabBarControllerDelegate: tabBarController:didSelectViewController:

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...
carendt242 is offline   Reply With Quote
Old 08-02-2009, 11:58 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default

Quote:
Originally Posted by carendt242 View Post
brianslick - the popToRoot did the trick. i had a few problems that were code-related but now it works. thanks...

can you show your code..
david_david is offline   Reply With Quote
Old 08-03-2009, 12:15 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 7
carendt242 is on a distinguished road
Default

Quote:
Originally Posted by david_david View Post
can you show your code..
made AppDelegate a UITabBarControllerDelegate

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];
and then overrode this function in AppDelegate:

Code:
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
	NSArray *a = [viewController popToRootViewControllerAnimated:YES];
	NSLog(@"*******DidSelectViewController");
}
works like a charm...

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.
carendt242 is offline   Reply With Quote
Old 08-03-2009, 12:23 AM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-03-2009, 12:32 AM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 7
carendt242 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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.

- chuck
carendt242 is offline   Reply With Quote
Old 08-03-2009, 12:33 AM   #10 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default

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

thanks to the responses...
david_david is offline   Reply With Quote
Old 08-03-2009, 12:39 AM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by carendt242 View Post
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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-21-2009, 06:28 AM   #12 (permalink)
iPhone app Developer
 
Join Date: Jul 2009
Location: India
Age: 26
Posts: 36
brian is on a distinguished road
Send a message via Yahoo to brian Send a message via Skype™ to brian
Default Same issue...plz help

I too got the same problem. And i did the codes as you suggested

Code:
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
	[viewController popToRootViewControllerAnimated:YES];
}
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.)
brian is offline   Reply With Quote
Old 09-30-2009, 03:18 PM   #13 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default

Quote:
Originally Posted by brian View Post
I too got the same problem. And i did the codes as you suggested

Code:
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
	[viewController popToRootViewControllerAnimated:YES];
}
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?
Argus is offline   Reply With Quote
Old 09-30-2009, 03:24 PM   #14 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You don't send the pop message to a view controller, you send it to a navigation controller.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-30-2009, 07:12 PM   #15 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You don't send the pop message to a view controller, you send it to a navigation controller.
Thanks for the quick reply Brian. I created a UINavigationController *navController in my AppDelegate and called this method:

Code:
- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController 
{
	NSLog(@"navController %@", navController);
	[navController popToRootViewControllerAnimated:NO];
}
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.
Argus is offline   Reply With Quote
Old 09-30-2009, 07:25 PM   #16 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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:

Code:
[[thatViewController navigationController] pop...];
See the UIViewController documentation for why this works.

Since you have a delegate method that is reporting a selected view controller, might as well use that:

Code:
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
	[[viewController navigationController] popToRootViewControllerAnimated:YES];
}
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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-01-2009, 12:42 PM   #17 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default

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.

BTW I like your app. Will start using it.
Argus is offline   Reply With Quote
Old 12-09-2009, 06:58 PM   #18 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default

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:

Code:
- (void)tabBarController:(UITabBarController*)tabBarController 
didEndCustomizingViewControllers: (NSArray*)viewControllers changed:(BOOL)changed {
}

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController {

if ([viewController isKindOfClass:[UINavigationController class]]) {
	[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
	}
}
Now, no matter where I've drilled down in any View Controller, if I hit a new Tab Bar item, it pops to the root view of that view.

Thanks again for pointing out that popToRootViewController only works with Navigation Controllers!!
Argus is offline   Reply With Quote
Old 12-03-2010, 04:25 PM   #19 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 1
gthomson is on a distinguished road
Default

Hi Argus,

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.


Code:
- (void)tabBarController:(UITabBarController*)tabBarController 
didEndCustomizingViewControllers: (NSArray*)viewControllers changed:(BOOL)changed {
}
Greg
gthomson is offline   Reply With Quote
Old 12-04-2010, 07:55 AM   #20 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 15
Argus is on a distinguished road
Default

That method was deleted. It wasn't necessary for me either. Glad you got your tab bar working.
Argus is offline   Reply With Quote
Old 12-08-2010, 10:22 AM   #21 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 21
Apocryphon is on a distinguished road
Default

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:

Code:
 - (void)viewDidLoad {
	 [super viewDidLoad];

	 [self.view addSubview:self.rootTabBarController.view];
	 [self.rootTabBarController.view setFrame:self.view.frame];
 }
How would I override the AppDelegate methods for this instance variable UITabBarController?
Apocryphon is offline   Reply With Quote
Old 12-08-2010, 10:55 AM   #22 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by Apocryphon View Post
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:

Code:
 - (void)viewDidLoad {
	 [super viewDidLoad];

	 [self.view addSubview:self.rootTabBarController.view];
	 [self.rootTabBarController.view setFrame:self.view.frame];
 }
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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
like in ipod app, pop to root, select another tab, uitabbarcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
10 members and 307 guests
chiataytuday, coolman, givensur, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,882
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:19 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0