【问题标题】:UITabBarController not showing all view controllersUITabBarController 未显示所有视图控制器
【发布时间】:2026-01-12 12:25:01
【问题描述】:

我的 UITabBarcontroller 有两个视图控制器 -最爱 -键盘

我将这两个控制器按收藏夹、键盘、nil 的顺序添加到一个数组中。

当应用程序启动时,只有收藏夹选项卡出现在选项卡栏中,我必须单击第二个选项卡才能让“键盘”(viewController 的标题)文本出现在选项卡上。

如何使标签栏在启动时具有两个视图控制器的标题?

【问题讨论】:

    标签: iphone objective-c uitabbarcontroller


    【解决方案1】:

    向我们展示您的 tabbarcontroller 初始化方法。它应该是直截了当的-

    -创建 1 个标签栏控制器

    -创建 2 个标签项

    -创建2个导航控制器

    -使用 navcontroller.tabBarItem 属性将 tabbaritems 分配给导航控制器

    -使用tabbarcontroller setViewControllers:animated: 函数将导航控制器添加到标签栏,然后将标签栏控制器添加到窗口。

    【讨论】:

    • 我发现了问题,我在其 ViewDidLoad 方法中声明了第二个视图控制器的标题。这就是为什么仅在我点击第二个标签栏时才显示标题的原因。现在我已经在 appdelegate 本身中声明了视图的标题,它显示标题而不点击它
    【解决方案2】:

    在您加载tabControllerViewController 中尝试此操作:

    -(void)viewDidLoad
    {
        [super viewDidLoad];
        // creating the tabController
        UITabBarController *tabBarController = [[UITabBarController alloc] init];
    
        NSArray* controllers = [NSArray arrayWithObjects: myViewController, nil];
    
        myViewController.title = @"Title";
    
        tabBarController.viewControllers = controllers;
        [controllers release];
        [self.view addSubview:tabBarController.view];
    }
    

    【讨论】:

    • @user757343:欢迎使用 *! 请将每一行代码缩进 4 个空格。谢谢!
    【解决方案3】:

    试试这个:

    -(id)setup
    {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"xxxx" image:[UIImage imageNamed:@"xxx.png"] tag:0];
        self.tabBarItem = item;
        [item release];
        return self;
    }
    
    
    
    -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            [self setup];
        }
        return self;
    }
    

    【讨论】:

      最近更新 更多