【问题标题】:self.title sets navigationController and tabBarItem's title? Why?self.title 设置navigationController 和tabBarItem 的标题?为什么?
【发布时间】:2010-12-05 04:07:50
【问题描述】:

我在 UIViewController 中为我的一个标签执行此操作:

self.title = @"Welcome";

但是,它会覆盖我对 tabBarItem 的所有内容。我试过了:

self.tabBarItem.title = @"Home";

[self.tabBarItem initWithTitle:@"Home" image:[UIImage imageNamed:@"iconHome.png"] tag:0];

但是,self.title 仍然会覆盖 tabBarItem,无论我是否在 设置标题后尝试后两段代码。代码甚至可以正常运行,但self.tabBarItem.titleinitWithTitle 什么都不做?

【问题讨论】:

    标签: iphone uinavigationcontroller tabbarcontroller


    【解决方案1】:

    好的,我想通了!这就是我正在做的事情:

    self.title = @"Title for TabBarItem"; // TabBarItem.title inherits the viewController's self.title
    self.navigationItem.title = @"Title for NavigationBar";
    

    navigationBar 将继承 self.title,除非使用 self.navigationItem.title 另行设置

    【讨论】:

    • 在 UITabController 中使用 UINavigationController 时,我遇到了完全相同的问题。这解决了它。
    • 这对我有用。非常适合仅根据内容更改顶部标题,即使视图控制器没有更改。
    • 快速注意这两行的顺序很重要。
    【解决方案2】:
    //set nav item title
    self.navigationController.navigationBar.topItem.title = @"zurück";
    

    这是为我做的 :=) (以上都不起作用)

    【讨论】:

      【解决方案3】:

      试试:

      [self setTitle:@"Welcome"];
      
      UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed: image] tag:0];
      [self setTabBarItem:item];
      [item release];
      

      【讨论】:

      • 嗯。也不行。 tabBar 的“Welcome”(自己的标题)总是相同的。实际上,非常令人沮丧。我与一位开发者朋友交谈,他说他能够做到这一点的唯一方法是在 IB 中设置 navigationController 的标题,然后不在代码中设置 self.title。但这似乎很奇怪。如果这里没有人提供解决方案,我可能会向 Apple 提交错误报告。
      • 不感兴趣 您在哪里设置标签栏?上面的示例来自我的控制器的 init 方法。我不为我的视图使用界面构建器(它们是自定义的或在 loadView 中创建的)。这可能就是区别。
      • 是的,我正在使用 IB,但尝试在每个 UIViewController (NavigationController) 中初始化选项卡。我将看看如果我将上面的代码放在 UIViewController 的 init 方法中会发生什么...
      【解决方案4】:

      我也面临同样的问题,但我是这样解决这个问题的。我在 appDelegate 中创建 tabBarItem 后立即设置了它们的标题和图像。

      这就是我所做的:

      [viewController setTitle:@"controllerTitle"];
      [[viewController tabBarItem] setTitle:@"Custome Title for tab"];
      [[viewController tabBarItem] setImage:[UIImage imageNamed:@"tab.png"]];
      

      【讨论】:

        猜你喜欢
        • 2011-10-16
        • 1970-01-01
        • 2010-11-20
        • 2011-03-23
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多