在UITabBarController
中,选择标签后,我希望该标签的UIViewController
更改(分配新的viewcontroller)。我正在尝试这个 -
NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];
但它给出了错误。如何分配新的UIViewController
并立即刷新?
答案 0 :(得分:2)
请参阅此代码,它提供2个带有导航的标签栏。
在AppDelegate.h
请声明
UINavigationController *nav1;
UINavigationController *nav2;
UITabBarController *tab;
在Appdelegate.m
,didFinishLaunchingWithOptions
中请添加: -
tab = [[UITabBarController alloc]init];
ViewController *view1 = [[ViewController alloc]init];
nav1= [[UINavigationController alloc]initWithRootViewController:view1];
UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTitle:@"Add" image:[UIImage imageNamed:@"Plus.png"] tag:1];
view1.title = @"Add";
[view1 setTabBarItem:tab1];
SettingsViewController *view2 = [[SettingsViewController alloc]init];
nav2= [[UINavigationController alloc]initWithRootViewController:view2];
UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTitle:@"Setting" image:[UIImage imageNamed:@"settings.png"] tag:2];
view2.title = @"Setting";
[view2 setTabBarItem:tab2];
tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nil];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = tab;
同时检查此链接以进一步实施...希望这有助于:)
答案 1 :(得分:1)
这是基于女性的答案,但不要求您构建整个视图控制器阵列,它只是让您将现有的视图控制器替换为另一个。在我的情况下,我想换一个不同的xib文件用于iPhone 5屏幕:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
Tracking *tracking568h = [[Tracking alloc] initWithNibName:@"Tracking-568h" bundle:nil];
tracking568h.title = [[viewControllers objectAtIndex:0] title];
tracking568h.tabBarItem = [[viewControllers objectAtIndex:0] tabBarItem];
[viewControllers replaceObjectAtIndex:0 withObject:tracking568h];
[tracking568h release];
[self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
这将更改第一个选项卡的视图控制器,保留相同的选项卡图标和标签。
答案 2 :(得分:0)
在AppDelegate.h
UIViewController *vc1;
UIViewController *vc2;
UIViewController *vc3;
Appdelegate.m
中的
didFinishLaunchingWithOptions
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
vc1 = [[UIViewController alloc] init];
vc1.title = @"A";
[listOfViewControllers addObject:vc1];
vc2 = [[UIViewController alloc] init];
vc2.title = @"B";
[listOfViewControllers addObject:vc2];
vc3 = [[UIViewController alloc] init];
vc3.title = @"C";
[listOfViewControllers addObject:vc3];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];