如何更改uitabBarItem的标题NIB文件或以编程方式

时间:2011-12-21 13:47:54

标签: iphone uitabbar

以下是在viewDidLoad和viewWillAppear中编写的代码行,但仍未更改uitabbar项目标题。

[[self.userTabBarController.viewControllers objectAtIndex:1] setTitle:@“你的标题”];

userTabBarController在哪里

IBOutlet  UITabBarController *userTabBarController;

我的标签栏有3个标签,这些标签与3个不同的viewController相连,并且加载正常。

为什么没有设置标题,代码似乎没问题。

6 个答案:

答案 0 :(得分:2)

试试这个:

MyViewController *viewController = [self.userTabBarController.viewControllers objectAtIndex:1] 

[viewController setTitle:@"Your title"];

由于这对您不起作用,我建议您在tabbarcontroller中查看视图控制器的实现文件,然后执行:

self.title = @"my title";

在viewDidLoad方法中。

答案 1 :(得分:0)

试试这个:

UITabBar *tabBar = self.userTabBarController.tabBar;
UITabBarItem *tabBarItem = [[tabBar items] objectAtIndex:0];
[tabBarItem setTitle:@"New Title"];

答案 2 :(得分:0)

在你的viewController的NIB,.m文件中。像你这样改变你的initWithNibName方法。将工作

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        [self setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Aktuelles" image:[UIImage imageNamed:@"News.png"] tag:0]];
        self.title = @"Aktuelles";
    }
    return self;
}

答案 3 :(得分:0)

使用awakeFromNib方法设置视图控制器的title属性:

- (void) awakeFromNib
{
   self.title = "My Title";
}

答案 4 :(得分:0)

您可以在IB上进行探索,或者如果您不想在appdelegate文件中进行设置,可以使用它:

[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"new title 1"];
[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:1] setTitle:@"new title 2"];
[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:2] setTitle:@"new title 3"];

编辑:因为它在我之前发布的人Xp

看起来是一样的

答案 5 :(得分:0)

 UITabBarItem *item = [self tabBarItem];
 item.title = @"Hi";