Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

iphone - UItabBar changing View Controllers

i have some difficulties changing tab bar controllers. Basically I have UITabBarController with 3 controllers. First time when app starts. I change one controller like this:

NSMutableArray *muteArray = [[NSMutableArray alloc] init];
FirstPage *online;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{

    online =[[FirstPage alloc] initWithNibName:nil bundle:nil];


}else{

    online =[[FirstPage alloc] initWithNibName:nil bundle:nil];
}

//adding all controllers of tab bar to array
[muteArray addObjectsFromArray:_navigationCotroller.viewControllers];
online.tabBarControllers = [muteArray copy];
//replacing object of login controller to after login controller
[muteArray replaceObjectAtIndex:1 withObject:online];


[online release];

//setting new controllers to tab bar
[_navigationCotroller setViewControllers:muteArray animated:YES];

[muteArray release];

Then in FirstPage controller i do some change and press OK. Now i need to change controllers again, doing this:

NSLog(@"Before change Tab Bar cotrollers = %@",self.tabBarController.viewControllers);

[self.tabBarController setViewControllers:_tabBarControllers animated:YES];

NSLog(@"After change Tab Bar cotrollers = %@",self.tabBarController.viewControllers);

[self.tabBarController.tabBarController setSelectedIndex:1];

_tabBarControllers is array of controllers which i saved when app started.

This code change controllers, but when i want to open changed controller with setSelectedIndex it don't work.

Any ideas ?

And print this:

Before change Tab Bar cotrollers = NULL After change Tab Bar cotrollers = NULL

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First I assume you meant:

[self.tabBarController setSelectedIndex:1];

Failing that it sounds like the problem is with your _tabBarControllers.

what do the following output:

NSLog(@" _tabBarControllers count = %d", [_tabBarControllers count]);
NSArray* newArray = [NSArray arrayWithArray:self.tabBarController.viewControllers];
NSLog(@" newArray count = %d", [newArray count]);

EDIT: Does the following successfully remove the first tab with no problems?

NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[newArray removeObjectAtIndex:0]; 
[self.tabBarController setViewControllers:newArray animated:YES];

EDIT 2 :

try changing:

[muteArray addObjectsFromArray:_navigationCotroller.viewControllers];
online.tabBarControllers = [muteArray copy];
[muteArray replaceObjectAtIndex:1 withObject:online];

to:

[muteArray addObjectsFromArray:self.tabBarController.viewControllers];
[muteArray replaceObjectAtIndex:1 withObject:online];
online.tabBarControllers = [muteArray copy];

To be honest I'm finding it hard to follow your app structure and object references.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...