2012-01-16 149 views
5

我改变标签栏控制器有一些困难。基本上我有3个控制器的UITabBarController。第一次当应用程序启动。我改变一个控制器是这样的:UItabBar更改视图控制器

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]; 

然后在第一页控制器我做了一些改变,然后按OK。现在我需要再次改变控制器,这样做:

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是控制器时,应用程序启动,我保存的阵列。

此代码更改控制器,但是当我想打开更改的控制器与setSelectedIndex它不起作用。

任何想法?

和打印这样的:

前更改标签栏cotrollers = NULL 后更改标签栏cotrollers = NULL

+0

_navigationCotroller也是你的代码中的错字? –

+0

_navigationCotroller是主要的UITabBarController – Streetboy

+0

介意那里缺少“N”字符;)_navigationCotroller - > _navigationCoNtroller –

回答

10

首先,我假设你的意思是:

[self.tabBarController setSelectedIndex:1]; 

做不到这听起来像问题在于你的_tabBarControllers。

什么做以下的输出:

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

编辑: 执行以下操作成功,没有任何问题删除第一个标签?

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

编辑2:

尝试改变:

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

到:

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

说实话,我发现很难按照你的应用程序结构和对象引用。

+0

_tabBarControllers数= 3 newArray数= 3 和 改变标签栏cotrollers之前=( “”, “<第一页:0x882b200 >”, “” ) 后改变标签栏cotrollers =(空) – Streetboy

+0

没有看到你的代码中创建_tabBarControll这很难提供帮助。 – ader

+0

上面有代码: online.tabBarControllers = [muteArray copy]; 这里我复制所有默认创建的控制器。 – Streetboy

相关问题