2012-01-19 37 views
31

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

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

你在哪里设置它? – Legolas

+0

我编辑我的问题,请现在检查 –

回答

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

它不工作仍然 –

+0

http://stackoverflow.com/questions/6361010/set-uitabbaritem-title-programmatically – glogic

+0

这是我的代码..:S –

3

试试这个

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

或者你也可以通过在一个给定的标签栏项目所显示的标题是由UITabBarItem对应的视图控制器的情况下确定这样

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

从文档判断,你没有办法在创建标签栏项目后设置标题。您需要改为制作一个新的。 – Caleb

4

设置标签栏。这些不是可变的,但是...如果你想改变标题(或图像或标签),你必须创建一个新项目并将其分配给视图控制器。

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

我试试这个但没有工作 - (IBAction)tab1Click:(id)sender { myTabBarController = [[UITabBarController alloc] init]; view2Controller = [[View2Controller alloc] init]; view3Controller = [[View3Controller alloc] init]; UITabBarItem * item2 = [[UITabBarItem alloc] initWithTitle:@“someTitle”image:nil tag:0]; view2Controller.tabBarItem = item2; myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme,view2Controller,view3Controller,nil]; [self.view addSubview:myTabBarController.view]; myTabBarController.selectedIndex = 1; } –

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"]; 
+0

它不工作:( –

8

一个简单的方法来做到这一点的图像:在您的viewController2viewDidLoad方法, 设置self.title = @"MyTitle";

+1

它不工作.. :( –

+0

当然,它应该工作!X-( – Legolas

+1

这个问题是,如果它是在viewDidLoad viewcontroller2,它将只会触发一次viewController2的选项卡已被点击,此时它会正确显示,但要解决它,以便它从正常显示正确,我们: [self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString( @“BotonMapas”,@“comment”)]; 和Aitul一样被列为他的答案 –

54

您可以以简单的方式设置所有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")]; 

雨燕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

+0

如何为每个UIViewController设置标题? – alexmorhun

+2

谢谢!这很有用。 – Raja

28

我如何做到这一点的实际视图控制器(未在App代表):

// 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

0

如果您在嵌入式UIViewController中调用此项,您可以更改父标签栏标题:

self.tabBarController.title = @“My 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; 
} 
相关问题