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

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

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 02-07-2010, 12:07 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 11
Question Dynamically Change the TabBar at Runtime

Good afternoon everyone. Thanks in advance to anyone who can help with this. I am officially stumped. Here is my problem.

I have a set of 4 tabs with views working great. My app has a second mode, called gameplay mode. In this mode, I want to display 4 completely different tabs with 4 new views. I am trying to find a way to dynamically switch between the two sets of tabs and controllers. Right now I have the follow code:

Code:
[tbc setViewControllers:tc animated:YES];
[tbc setSelectedViewController:[tc objectAtIndex:0]];
tbc is a TabBarController
tc is an NSMutableArray of ViewControllers

However this code simply empties the tab bar, and gives me a blank screen.

I tried including
Code:
[window addSubview:tbc.view];
in there as well, but that was a bust.

I tried adjusting the tabbar items by hand by using
Code:
[[tbc tabBar] setItems:ti animated:YES];
where ti is an array of items. That threw an exception saying this action was not allowed.

Has anyone ever gotten this to work? Has anyone been able to dynamically add a tab and it's controller at runtime? Thanks again for any help given. This has been a surprisingly difficult problem.
ggalante is offline   Reply With Quote
Old 02-07-2010, 08:22 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 11
Exclamation

SOLVED!

So here's the important stuff I was doing wrong. First make sure to alloc and init the array and views when you build your controllers. Here's how I built the array:
Code:
MyView1* vc1 = [[MyView1 alloc] init];
MyView2* vc2 = [[MyView2 alloc] init];
gpTabControllers = [[NSMutableArray alloc] init];
[gpTabControllers addObject:vc1];
[gpTabControllers addObject:vc2];
When you make the change to the new group of tabs, do this:
Code:
NSMutableArray *tc = gpTabControllers;
[tbc setViewControllers:tc animated:YES];
[window addSubview:tbc.view];
If you want to switch back, I would store the first group of controllers into another array. This might keep a lot of stuff in memory though.

Finally, you want to do the TabItems in the controllers themselves, which was where I was getting really confused. Overloading the init function in the MyView classes will do this. Here's a peak at what I added to those classes:
Code:
- (id)init {
	if (self = [super initWithNibName:@"MyView1" bundle:nil]) {
		self.title = @"Tab Item Title";
		
		UIImage* anImage = [UIImage imageNamed:@"tabItemImg.png"];
		UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Tab Item Title" image:anImage tag:0];
		self.tabBarItem = theItem;
		[theItem release];
	}
	return self;
}
I hope this helps someone else avoid a day long confusion spree like me. Thanks to everyone who was thinking about helping on this one, and thanks to everyone who helps on this site. It is always a big help for me.
ggalante is offline   Reply With Quote
Reply

Bookmarks

Tags
change tabs, dynamic, tab bar application, tabbarcontroller, user interface

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
» Stats
Members: 41,860
Threads: 49,768
Posts: 213,054
Top Poster: BrianSlick (3,138)
Welcome to our newest member, gustavo7sexton
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0