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

时间:2022-08-26 21:39:30

I want to set title to tab item programatically, but it not works. My code is below:

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

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

9 个解决方案

#1


56  

You can set all the UITabBar icons in an easy way. You can do this in your viewWillAppear: method:

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

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")

#2


29  

How I do it in the actual View Controller (not the App Delegate):

我是如何在实际的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

#3


8  

An easy way to do this : In your viewController2 's viewDidLoad method, set self.title = @"MyTitle";

一种简单的方法:在viewController2的viewDidLoad方法中,设置self.title = @“MyTitle”;

#4


4  

The title displayed on a given tab bar item is determined by the corresponding view controller's instance of UITabBarItem. Those aren't mutable, though... if you want to change the title (or image, or tag), you have to make a new item and assign it to the view controller.

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

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

#5


4  

[view2Controller setTitle:@"ImATitle"];

might be what your after

可能是你的追求

edit: ok i just tested this and it def works for me so give it a go

编辑:好吧,我刚测试了这个,它def对我有效,所以试一试

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

#6


3  

try this

尝试这个

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

or also you can set tab bar by in this way

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

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

#7


2  

first declare UITabBarDelegate

首先声明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;
}

and even you can set tab images using

甚至你可以使用设置标签图像

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

#8


0  

If you are calling this from within an embedded UIViewController, you can change the parent tab bar title with:

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

self.tabBarController.title = @"My Title.";

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

#9


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

#1


56  

You can set all the UITabBar icons in an easy way. You can do this in your viewWillAppear: method:

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

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")

#2


29  

How I do it in the actual View Controller (not the App Delegate):

我是如何在实际的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

#3


8  

An easy way to do this : In your viewController2 's viewDidLoad method, set self.title = @"MyTitle";

一种简单的方法:在viewController2的viewDidLoad方法中,设置self.title = @“MyTitle”;

#4


4  

The title displayed on a given tab bar item is determined by the corresponding view controller's instance of UITabBarItem. Those aren't mutable, though... if you want to change the title (or image, or tag), you have to make a new item and assign it to the view controller.

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

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

#5


4  

[view2Controller setTitle:@"ImATitle"];

might be what your after

可能是你的追求

edit: ok i just tested this and it def works for me so give it a go

编辑:好吧,我刚测试了这个,它def对我有效,所以试一试

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

#6


3  

try this

尝试这个

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

or also you can set tab bar by in this way

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

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

#7


2  

first declare UITabBarDelegate

首先声明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;
}

and even you can set tab images using

甚至你可以使用设置标签图像

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

#8


0  

If you are calling this from within an embedded UIViewController, you can change the parent tab bar title with:

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

self.tabBarController.title = @"My Title.";

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

#9


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