【问题标题】:Add titles to tabbar created programmatically向以编程方式创建的标签栏添加标题
【发布时间】:2011-07-15 04:41:34
【问题描述】:

我是 iPhone 开发的新手...现在我正在尝试构建一个应用程序,我在其中以编程方式创建 UITabbarController,如下所示:

UITabBarController *tabbar = [[UITabBarController alloc] init];
firstViewController  *firstView = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];

UINavigationController *tabItemOne = [[[UINavigationController alloc] initWithRootViewController: firstView] autorelease];
secondViewController *secondView = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];

UINavigationController *tabItemTwo = [[[UINavigationController alloc] initWithRootViewController: settings] autorelease];
tabbar.viewControllers = [NSArray arrayWithObjects:tabItemOne, tabItemTwo,nil]; 
tabbar.view.frame = CGRectMake(0,0,320,460);

[self.view insertSubview:tabbar.view belowSubview: firstView.view];
[self presentModalViewController:tabbar animated:NO];

在此,我如何为tabbar 和那些控制器添加标题。 我试过了:

firstView.title = @"First View";

tabItemOne.title = @"First View";

但这两个不起作用..我该怎么做?

【问题讨论】:

  • 为什么要在firstView的视图下面插入子视图?

标签: iphone objective-c ios uitabbarcontroller tabbarcontroller


【解决方案1】:

设置aViewController.title 设置navigationItemtabBarItemtitle。如果您希望 navigationItemtabBarItem 具有不同的 title,请执行此操作,

// First set the view controller's title
aViewController.title = @"First View Tab";

// Then set navigationItem's title
aViewController.navigationItem.title = @"First View";

【讨论】:

  • 终于成功了..我将代码从 viewDidLoad 移到 viewDidAppear 并且成功了..非常感谢..
【解决方案2】:

要设置控制器标签的标签,请执行以下操作:

tabItemOne.tabBarItem.title = @"First View";

(顺便说一句,您可能需要重新考虑您的变量名称。它们非常令人困惑。tabbar 听起来应该是 UITabBar 的一个实例,而它实际上是一个 UITabBarController。firstView 和 secondView 听起来像 UIView 的实例它们实际上是 UIViewController 子类的实例。tabItemOne 和 tabItemTwo 听起来像 UITabBarItem 的实例,而它们实际上是 UINavigationController 的实例。)

【讨论】:

  • 它有帮助.. 实际上我修改了命名以将代码放在这里.. 在我的代码中,我将其命名为仅与我们的产品相关.. 无论如何谢谢..
  • 终于成功了。我将代码从 viewDidLoad 移动到 viewDidAppear 并且成功了。非常感谢。
【解决方案3】:
UITabBarItem *item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:0];
       //item.image=[UIImage imageNamed:@"hometab.png"];
       item.title=@"First View";

    item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:1];
       //item.image=[UIImage imageNamed:@"hometab.png"];
       item.title=@"Second View";

【讨论】:

  • 终于成功了。我将代码从 viewDidLoad 移动到 viewDidAppear 并且成功了。非常感谢。
【解决方案4】:
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"First View" image:[UIImage imageNamed:@"First View.png"] tag:0];
[tabItemOne setTabBarItem:item];
[item release];

如果我是对的。

【讨论】:

  • 为什么不起作用?它崩溃了吗?标题没显示?您是否将其设置为您添加到数组中的控制器?
  • 这是方法的签名,确保它与您的代码中的签名 - (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag;
  • 您不必设置视图控制器的 tabBarItem 属性。 UIViewController 延迟加载此属性,在您第一次访问此属性时自动为您创建一个 tabBarItem。所以你可以简单地做controller.tabBarItem.title = @"Tab Title";
  • 没有tabBarItem.title 这样的东西。此外,使用该方法,您还可以设置图像。
  • 终于成功了。我将代码从 viewDidLoad 移到 viewDidAppear 并且成功了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 2011-07-07
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多