如何在目标c中以编程方式设置标签栏项目标题?

Hac*_*kac 36 xcode objective-c uitabbarcontroller uitabbaritem

我想以编程方式将标题设置为标签项,但它不起作用.我的代码如下:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Ait*_*tul 63

您可以轻松设置所有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")];
Run Code Online (Sandbox Code Playgroud)

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")
Run Code Online (Sandbox Code Playgroud)

  • 哪个视图控制器的`viewWillAppear:`? (6认同)
  • 谢谢!这很有用. (2认同)

Rob*_*Rob 29

我是如何在实际的View Controller(而不是App Delegate)中完成的:

// 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
Run Code Online (Sandbox Code Playgroud)


Leg*_*las 8

一个简单的方法:在你viewController2viewDidLoad方法中,设置self.title = @"MyTitle";


glo*_*gic 5

[view2Controller setTitle:@"ImATitle"];
Run Code Online (Sandbox Code Playgroud)

可能是你的追求

编辑:好的,我刚刚测试了这个,它对我来说很有效,所以就去吧

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];
Run Code Online (Sandbox Code Playgroud)