如何在Objective-C中以编程方式设置选项卡栏项目标题?

37

我想通过编程方式设置选项卡项目的标题,但它不起作用。 我的代码如下:

- (IBAction)tab1Click:(id)sender {
    myTabBarController = [[UITabBarController alloc] init];        
    view2Controller = [[View2Controller alloc] init]; 
    [view2Controller setTitle:@"title"];
    view3Controller = [[View3Controller alloc] init];  
    deneme = [[ViewController alloc] init];  

    myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme, view2Controller,view3Controller, nil]; 
    [self.view addSubview:myTabBarController.view];    
    myTabBarController.selectedIndex=1;
}

我编辑了我的问题,请现在检查一下。 - Hacer sengul Akac
10个回答

66

你可以简单地设置所有UITabBar图标。你可以在你的viewWillAppear:方法中实现:

[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"BotonMapas", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonRA", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(@"BotonEstado", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(@"LabelInfo", @"comment")];

Swift 3.1 解决方案

self.tabBarController?.tabBar.items?[0].title = NSLocalizedString("BotonMapas", comment: "comment")
self.tabBarController?.tabBar.items?[1].title = NSLocalizedString("BotonRA", comment: "comment")
self.tabBarController?.tabBar.items?[2].title = NSLocalizedString("BotonEstado", comment: "comment")
self.tabBarController?.tabBar.items?[3].title = NSLocalizedString("LabelInfo", comment: "comment")

6
viewWillAppear: 方法对应哪个视图控制器? - fatuhoku
如何为每个UIViewController设置标题? - alexmorhun

29

我如何在实际的视图控制器中完成它(而不是在应用程序委托中):

// set tab title
self.title = @"Tab Title";
// optionally, set different title for navigation controller
self.navigationItem.title = @"Nav Title";
// Note: self.title will reset Nav Title. Use it first if you want different titles

1
为导航控制器设置不同的标题加1分!我需要将其大写,仅在导航栏上显示。 - nmdias

8
一个简单的方法:在您的viewController2viewDidLoad方法中,设置self.title = @"我的标题";

1
问题在于,如果它在viewcontroller2的viewDidLoad中,它只会在点击viewController2的选项卡后触发一次。此时它将正确显示,但为了修复它,使其从一开始就正确显示,我们可以使用以下代码:[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonMapas", @"comment")];就像Aitul在他的答案中列出的那样。 - Steven Marlowe

5
[view2Controller setTitle:@"ImATitle"];

也许是您需要的。

编辑: 好的,我刚刚测试了一下,它对我来说绝对有效,所以试一下吧。

UINavigationController *nav1 = [[UINavigationController alloc] init]; 
myViewController *myView = [[myViewController alloc] init];
//myView.title = @"Title"; //prob not needed
[nav1 pushViewController: myView  animated:NO];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage      imageNamed:@"title.png"] tag:0];
nav1.tabBarItem = item;
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nav1, nil];

https://dev59.com/ZljUa4cB1Zd3GeqPP0dU - glogic
当我点击按钮时,没有任何反应。 - Hacer sengul Akac
抱歉,您仍需要将TBC添加到窗口或视图中。 - glogic

4

试一试

[(UIViewController *)[tabBarController.viewControllers objectAtIndex:Index] setTitle:@"Title"]; 

或者你也可以通过以下方式设置选项卡栏。
UITabBarItem *tabItem = [[[tabBarController tabBar] items] objectAtIndex:INDEX];
[tabItem setTitle:@"TITLEe"];

从文档上来看,创建完标签栏项目后无法设置其标题。只能新建一个标签栏项目来替换原先的。 - Caleb

4

给定标签栏项目上显示的标题由相应的视图控制器的UITabBarItem实例确定。然而,这些实例是不可变的...如果你想改变标题(或图片、标签),你必须创建一个新项目并将其分配给视图控制器。

UITabBarItem *item2 = [[UITabBarItem alloc initWithTitle:@"someTitle" image:someImage tag:0];
viewController2.tabBarItem = item2;

我尝试了这个,但没有起作用 - (IBAction)tab1Click:(id)sender { myTabBarController = [[UITabBarController alloc] init]; view2Controller = [[View2Controller alloc] init]; view3Controller = [[View3Controller alloc] init]; UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"一些标题" image:nil tag:0]; view2Controller.tabBarItem = item2; myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme,view2Controller,view3Controller, nil]; [self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=1;
}
- Hacer sengul Akac

2

首先声明UITabBarDelegate

- (IBAction)tab1Click:(id)sender {

    myTabBarController = [[UITabBarController alloc] init]; 

    myTabBarController.delegate = self;

    view2Controller = [[View2Controller alloc] init]; 
    [view2Controller setTitle:@"title"];
    view3Controller = [[View3Controller alloc] init];  
    deneme = [[ViewController alloc] init];  
    dename.title = @"Dename";
    view2Conreoller.title = @"View2";
    view3Conreoller.title = @"View3";
    myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme, view2Controller,view3Controller, nil]; 
    [self.view addSubview:myTabBarController.view];    
    myTabBarController.selectedIndex=1;
}

甚至您可以使用以下方法设置选项卡图像:

view2Controller.tabBarItem.image = [UIImage imageNamed:@"misle.png"];

1
在Swift中,您可以通过以下方式在viewDidLoad:方法中更改UITabBarController项目的标题:
   self.viewControllers![0].title="Deposit"
   self.viewControllers![1].title="Withdraw"
   self.viewControllers![2].title="Activity"

1
如果您正在从嵌入的UIViewController中进行调用,您可以使用以下代码更改父选项卡栏标题:
self.tabBarController.title = @"我的标题。";

0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    UITabBarController *tb=[[UITabBarController alloc]init];
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main"   bundle:nil];
    UIViewController *vc1=[sb   instantiateViewControllerWithIdentifier:@"View1"];
    UIViewController *vc2=[sb   instantiateViewControllerWithIdentifier:@"View2"];

      //To Set Title for UIViewController

      [vc1 setTitle:@"View1"];
      [vc2 setTitle:@"View2"];


    NSArray *vController=[[NSArray alloc]initWithObjects:vc1,vc2,nil];
    tb.viewControllers=vController;
    self.window.rootViewController=tb;
    [self.window makeKeyAndVisible];


  return YES;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接