以编程方式设置 UITabBar 标题
Posted
技术标签:
【中文标题】以编程方式设置 UITabBar 标题【英文标题】:Programmatically Setting UITabBar Titles 【发布时间】:2011-05-19 09:15:54 【问题描述】:我以编程方式添加了标签栏,如下所示:-
FirstViewController *obj_FirstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; SecondViewController *obj_SecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; ThirdViewController *obj_ThirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
navigation1 = [[UINavigationController alloc] initWithRootViewController:obj_FirstViewController];
navigation2 = [[UINavigationController alloc] initWithRootViewController:obj_SecondViewController];
navigation3 = [[UINavigationController alloc] initWithRootViewController:obj_ThirdViewController];
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate=self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:navigation1,navigation2,navigation3,nil]];
MainTabBar.view.frame=self.view.frame;
MainTabBar.selectedIndex=0;
[self.view addSubview:MainTabBar.view]
通过在(void)viewDidLoad
中编写此内容,我在视图控制器中获得了 3 选项卡。但问题是我想将选项卡的名称设置为 1)Home 2)Favorites 3)About us
我曾尝试编写以下代码:
- 1)obj_FirstViewController.tabBarItem.title=@"Home"; 2)self.title = @"My View Controller";
但这不起作用。谁能帮助我如何以编程方式执行此操作。在哪里写行,以便我在标签栏中获得这 3 个名称
【问题讨论】:
仅供参考:这不是“xcode”问题:Xcode 是集成开发环境,可用于在多种平台上以多种语言进行编程。这是一个 Cocoa Touch 问题。 【参考方案1】:试试这个方法……
NSMutableArray *controllers = [[NSMutableArray alloc] init];
FirstViewController *obj_FirstViewController = [[FirstViewController alloc] init];
[obj_FirstViewController setTitle:@"first"];
UITabBarItem *item = [[[UITabBarItem alloc] setTabBarItem: [[[UITabBarItem alloc] initWithTitle: @"First") image:[UIImage imageNamed:@"first.png"] tag:2] autorelease]];
[obj_FirstViewController setTabBarItem:item];
[controllers addObject:obj_FirstViewController];
[obj_FirstViewController release];
【讨论】:
【参考方案2】:请试试这个
FirstViewController *obj_FirstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *obj_SecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *obj_ThirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
navigation1 = [[UINavigationController alloc] initWithRootViewController:obj_FirstViewController];
navigation2 = [[UINavigationController alloc] initWithRootViewController:obj_SecondViewController];
navigation3 = [[UINavigationController alloc] initWithRootViewController:obj_ThirdViewController];
navigation1.title=@"Home";
navigation2.title=@"Second";
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate=self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:navigation1,navigation2,navigation3,nil]];
MainTabBar.view.frame=self.view.frame;
MainTabBar.selectedIndex=0;
[self.view addSubview:MainTabBar.view]
【讨论】:
以上是关于以编程方式设置 UITabBar 标题的主要内容,如果未能解决你的问题,请参考以下文章
当 UITabBar 不是 rootViewController 时,如何以编程方式将 UITabBar 与具有 NIB 的不同 ViewController 链接