OStack程序员社区-中国程序员成长平台

标题: iphone - 更改 UITabBarController 的 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 17:22
标题: iphone - 更改 UITabBarController 的 View Controller

UITabBarController 中,选择一个选项卡后,我希望该选项卡的 UIViewController 发生变化(分配一个新的 View Controller )。我正在尝试这个-

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

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



Best Answer-推荐答案


这是基于 femina 的回答,但不需要您构建整个 View Controller 阵列,它只是让您可以用另一个替换现有的 View Controller 。就我而言,我想为 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];
}

这会更改第一个选项卡的 View Controller ,保持相同的选项卡图标和标签。

关于iphone - 更改 UITabBarController 的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13024253/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4