问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/8878623/uitabbar-changing-view-controllers