3

在 aUITabBarController中,选择一个选项卡后,我希望该选项卡UIViewController更改(分配一个新的视图控制器)。我正在尝试这个-

NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];

但它给出了错误。如何分配一个新的UIViewController并立即刷新?

4

3 回答 3

3

这是基于 femina 的回答,但不需要您构建整个视图控制器数组,它只是让您用另一个替换现有的视图控制器。就我而言,我想为 iPhone 5 屏幕换一个不同的 xib 文件:

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

这会更改第一个选项卡的视图控制器,保持相同的选项卡图标和标签。

于 2013-04-02T20:13:43.433 回答
2

请参阅此代码,它提供了 2 个带导航的标签栏。在AppDelegate.h请声明

    UINavigationController *nav1;
    UINavigationController *nav2;
    UITabBarController *tab;

Appdelegate.mdidFinishLaunchingWithOptions请添加:-

    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;

还要检查此链接以进一步实施...希望这会有所帮助:)

UItabBar 改变视图控制器

于 2012-10-23T05:54:14.080 回答
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];
于 2012-10-23T07:17:44.770 回答