Iphone 如何更改uitabBarItem的标题(NIB文件或程序化)

Iphone 如何更改uitabBarItem的标题(NIB文件或程序化),iphone,uitabbar,Iphone,Uitabbar,这是在viewDidLoad中编写的代码行,ViewWill将出现,但uitabbar项目标题仍没有更改 [[self.userTabBarController.viewControllers objectAtIndex:1]setTitle:@“您的标题”] 其中userTabBarController是 IBOutlet UITabBarController *userTabBarController; 我的选项卡栏有3个选项卡,它们与3个不同的viewController连接在一起,并

这是在viewDidLoad中编写的代码行,ViewWill将出现,但uitabbar项目标题仍没有更改

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

其中userTabBarController是

IBOutlet  UITabBarController *userTabBarController;
我的选项卡栏有3个选项卡,它们与3个不同的viewController连接在一起,并且加载良好

为什么没有设置标题,代码看起来没问题。

试试这个:

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

[viewController setTitle:@"Your title"];
UITabBar *tabBar = self.userTabBarController.tabBar;
UITabBarItem *tabBarItem = [[tabBar items] objectAtIndex:0];
[tabBarItem setTitle:@"New Title"];
由于这对您不起作用,我建议您转到tabbarcontroller中视图控制器的实现文件,然后简单地执行以下操作:

self.title = @"my title";
在viewDidLoad方法中。

尝试以下操作:

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

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

在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;
}

在其
awakeFromNib
方法中设置视图控制器的title属性:

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

您可以在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的人看起来是一样的,不,它不起作用。在哪里写这行代码,我的意思是在哪个函数中?@Ajay_Kumar我建议然后转到ViewController实现文件,在viewDidLoad do self.title=@“my title”我的第一个选项卡ViewController名称是homescreenvc,我写了'self.title=@“my title”;'在viewDidload中,但没有发生任何事情,我仍然没有看到标题。即使我已经将每个选项卡的标题也放在nib文件中,但也没有从那里加载。我在viewDidload中编写了上述代码,但仍然没有发生任何事。我是不是在其他地方做错了,还是这是一个普遍的问题?我搜索了所有地方,所有代码看起来都一样。调试并查看是否看到tabBarItem的有效地址。然后在控制台上键入这个(在调试模式下):po tabBarItem.title并查看它显示的字符串…我发现它没有在所选选项卡所属的viewcontroller中调用viewDidload、ViewDidDisplay、ViewWillDisplay和awakeFromNib。但是XIB文件正在加载,因为designig在屏幕上显示得很好。很好,您找到了解决方案。如果有助于你解决问题,请记下我的答案。
 UITabBarItem *item = [self tabBarItem];
 item.title = @"Hi";