在这里,我试图从uitabbarcontroller动态添加和删除选项卡 .
以及替换它们 . 我的应用程序情况如下 .
第一个视图是Logon和about
成功登录新的3个选项卡后 . 随着更多和另外两个 .
在更多注销选项是选择此后我想删除最后一个选项卡和
再次登录和关于替换前两个 . 即(主视图)
所以在我的代码中我这样做........
///AppDelegate.m
- (BOOL)应用程序:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//在应用程序启动后覆盖自定义点 .
self.window.rootViewController = tabBarCntr; tabBarCntr = [[UITabBarController alloc] init]; LogonViewController * logon = [[LogonViewController alloc] init]; logon.tabBarItem.title = @ “登录”; AboutViewController * about = [[AboutViewController alloc] init]; about.tabBarItem.title = @ “关于”; tabBarCntr.viewControllers = [NSArray arrayWithObjects:sos1,about,nil]; [登录发布]; [关于发布];
[self.window addSubview:tabBarCntr.view];
[self.window makeKeyAndVisible]; }
登录成功后,在我的IBAction中将新的viewcontrollers设置为tabbarcontroller
self.tabBarController.viewControllers = [NSArray的
arrayWithObjects:newController1,newController2,更多,零] .
Now in my loggoff i coding something like.....
NSMutableArray *newArray;
newArray=[NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[newArray replaceObjectAtIndex:0 withObject:logon];
[newArray replaceObjectAtIndex:1 withObject:about];
[newArray removeLastObject];
self.tabBarController.selectedIndex=0;
[self.tabBarController setViewControllers : newArray];
but my applcation crash here after this ....
can any has solution for this ...
Thnx提前 .
Paggyyyyy
编辑:NSMutableArray arrayWithArray添加了修复代码的空间
2 回答
在我看来,你使用2个单独的tabBarControllers . 一个用于登录,另一个用于app . 登录后删除并释放登录选项卡并添加应用程序选项卡 . 在注销后删除并释放应用程序选项卡并添加登录选项卡 . 这将更容易访问选项卡,而不是删除/添加视图控制器,重命名选项卡,更改选项卡图像 .
您可以通过属性setViewControllers:animated:YES修改标签栏控制器 . 您可以在那里分配一个新数组 . 它相应地切换 .
[tabBarController setViewControllers:array animated:YES];
有一个重要的问题 . 我使用手动引用计数,但我使用自动释放选项分配给此数组的视图控制器,因为我使用Xcode模板作为应用程序基础 . 这些仅由该数组保留 . 如果您忘记保留在应用程序生命周期中可能出现在不同选项卡阵列中的所有视图控制器,则在更改,更改或甚至阵列初始化期间会出现奇怪的内存崩溃 . 因此有必要保留对这些视图控制器的引用/