UITabBar 标题未显示

发布于 2024-12-22 12:26:23 字数 1401 浏览 1 评论 0原文

我已经完全按照 Apple 推荐的方式以编程方式实现了 UITabBar,但我无法显示标题。这是我实现它的方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    MainViewController *mainViewController = [[MainViewController alloc] init];
    UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
    [[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack];

    [tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController, nil]];

    [self.window setRootViewController: tabBarController];
    [self.window makeKeyAndVisible];

    [mainViewController release];
    [mainNavigationController release];

    [tabBarController release];

    return YES;
}

这是 mainViewController 的 init 方法,我在其中添加标题:

@implementation MainViewController

    - (id) init
    {
        self = [super initWithNibName: nil bundle: nil];

        if (self)
        {
          UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Main" image: nil tag: 1];
          [self setTabBarItem: tabBarItem];
          [tabBarItem release];
        }

        return self;
    }

我注意到还有很多其他问题,有些有 hacky 类型的解决方案,但我想知道苹果是否推荐这样,为什么它不起作用?

I've implemented a UITabBar programmatically exactly the way Apple recommends, and I can't get the title to show up. Here is how I'm implementing it:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    MainViewController *mainViewController = [[MainViewController alloc] init];
    UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
    [[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack];

    [tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController, nil]];

    [self.window setRootViewController: tabBarController];
    [self.window makeKeyAndVisible];

    [mainViewController release];
    [mainNavigationController release];

    [tabBarController release];

    return YES;
}

Here is the init method of mainViewController where I add the title:

@implementation MainViewController

    - (id) init
    {
        self = [super initWithNibName: nil bundle: nil];

        if (self)
        {
          UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Main" image: nil tag: 1];
          [self setTabBarItem: tabBarItem];
          [tabBarItem release];
        }

        return self;
    }

I noticed there are a bunch of other questions on this, and some have hacky type solutions, but I'm wondering if Apple is recommending this way, why isn't it working??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-12-29 12:26:23

这可以通过多种方式来完成。最简单的方法是尝试设置

self.title = @"Main";

这应该反映在您的 navigationBar 标题和 tabBarItem 标题中。
或者,您可以将它们连接到界面生成器并自己输入并正确连接插座。

This can be done in a variety of ways. The simplest one is to try setting

self.title = @"Main";

This should be reflected in both your navigationBar title and tabBarItem title.
Or , you could just connect them to the interface builder and type it out yourself and connect the outlets properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文