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

时间:2012-01-19 12:22:25

标签: objective-c xcode 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;
}

10 个答案:

答案 0 :(得分:61)

您可以轻松设置所有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")

答案 1 :(得分: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

答案 2 :(得分:8)

一种简单的方法:在viewController2的{​​{1}}方法中,  设置viewDidLoad

答案 3 :(得分:4)

[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];

答案 4 :(得分:4)

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

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

答案 5 :(得分:3)

试试这个

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

或者您也可以通过这种方式设置标签栏

UITabBarItem *tabItem = [[[tabBarController tabBar] items] objectAtIndex:INDEX];
[tabItem setTitle:@"TITLEe"];

答案 6 :(得分: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"];

答案 7 :(得分:1)

如果您是在嵌入式UIViewController中调用它,则可以使用以下命令更改父标签栏标题:

self.tabBarController.title = @"我的标题。";

答案 8 :(得分: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;
}

答案 9 :(得分:0)

您可以在 viewDidLoad:方法中迅速更改 UITabBarController 项目的标题,如下所示

   self.viewControllers![0].title="Deposit"
   self.viewControllers![1].title="Withdraw"
   self.viewControllers![2].title="Activity"