【问题标题】:How to set tab bar item title programmatically in objective c?如何在目标c中以编程方式设置标签栏项目标题?
【发布时间】:2012-02-14 02:32:33
【问题描述】:

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

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

【问题讨论】:

  • 我编辑了我的问题,请立即查看

标签: objective-c xcode uitabbarcontroller uitabbaritem


【解决方案1】:

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

【讨论】:

  • 哪个视图控制器的viewWillAppear:
  • 如何为每个 UIViewController 设置标题?
【解决方案2】:

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

【讨论】:

  • +1 为导航控制器设置不同的标题!我需要将其设为大写,仅在导航栏上。
【解决方案3】:

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

【讨论】:

  • 这个问题是,如果它在 viewcontroller2 的 viewDidLoad 中,它只会在 viewController2 的选项卡被点击后触发。此时它将正确显示,但要修复它以便从一开始就正确显示,我们: [self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonMapas", @"comment" )];就像 Aitul 被列为他的答案一样。
【解决方案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];

【讨论】:

【解决方案5】:

给定标签栏项目上显示的标题由相应视图控制器的 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:@"someTitle" image:nil tag:0]; view2Controller.tabBarItem = item2; myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme,view2Controller,view3Controller, nil]; [self.view addSubview:myTabBarController.view]; myTabBarController.selectedIndex=1; }
【解决方案6】:

试试这个

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

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

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

【讨论】:

  • 从文档来看,创建后无法设置标签栏项目的标题。你需要换一个新的。
【解决方案7】:

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

【讨论】:

    【解决方案8】:

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

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

    【讨论】:

      【解决方案9】:

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

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

      【讨论】:

        【解决方案10】:
        - (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;
        }
        

        【讨论】:

          猜你喜欢
          • 2016-02-19
          • 2012-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多