【问题标题】:UItabBar changing View ControllersUItabBar 改变视图控制器
【发布时间】:2012-01-16 10:22:39
【问题描述】:

我在更改标签栏控制器时遇到了一些困难。基本上我有带有 3 个控制器的 UITabBarController。应用程序启动时的第一次。我像这样更改一个控制器:

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

然后在 FirstPage 控制器中我进行一些更改并按 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 打开更改后的控制器时,它不起作用。

有什么想法吗?

然后打印出来:

在更改 Tab Bar 之前 cotrollers = NULL 更改后 Tab Bar 控制器 = NULL

【问题讨论】:

  • _navigationCotroller 也是您代码中的错字吗?
  • _navigationCotroller 是主要的 UITabBarController
  • 请注意那里缺少的“N”字符;) _navigationCotroller --> _navigationCoNtroller
  • 调用tabBarController navigationController 有点混乱。阅读本文的每个人都会假设 navigationController 是一个导航控制器。我建议您将 tabBarController 称为 myTabBarController

标签: iphone objective-c ios uitabbarcontroller uitabbar


【解决方案1】:

首先我假设你的意思是:

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

说实话,我发现很难遵循您的应用程序结构和对象引用。

【讨论】:

  • _tabBarControllers count = 3 newArray count = 3 and Before change Tab Bar cotrollers = ( "", "", "" ) 更改后标签栏控制器 = (null)
  • 如果没有看到您创建 _tabBarControllers 的代码,就很难提供帮助。
  • 上面有代码:online.tabBarControllers = [muteArray copy];在这里我复制所有默认创建的控制器。
  • 是的,上面的代码删除第一个标签栏控制器没有问题
  • 我有 UItabBarController 和 UINavigationController 的里面。这一行 NSLog(@"---> %@",[[appDelegate.navigationTabBarCotroller.viewControllers objectAtIndex:1] viewControllers]); ---> ("", "" ) 打印:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多