UITabBarController 不显示 tabbarItems 名称
我正在创建基于 UTabbarController 的应用程序。我已经以编程方式创建了该选项卡栏。一切运行正常,除了我看不到 tabBatItem 标题。我已经正确初始化了所有内容,但是当应用程序启动时,我只能看到第一个选项卡栏标题。但如果我选择第二个选项卡项目等,我可以看到他们的名字。我不知道这里出了什么问题。这是我的代码。如果我犯了任何错误,请告诉我。
谢谢。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
HomeViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController*navController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navController1.title=@"Home";
[viewController1 release];
TrainerTableViewController *viewController2 = [[TrainerTableViewController alloc] initWithNibName:@"TrainerTableViewController" bundle:nil];
UINavigationController*navController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navController1.title=@"Trainer";
[viewController2 release];
SettingsTableViewController *viewController8 = [[[SettingsTableViewController alloc] initWithNibName:@"SettingsTableViewController" bundle:nil] autorelease];
UINavigationController*navController8=[[[UINavigationController alloc]initWithRootViewController:viewController8]autorelease];
navController1.title=@"Settings";
AboutUsViewController *viewController9 = [[[AboutUsViewController alloc] initWithNibName:@"AboutUsViewController" bundle:nil] autorelease];
UINavigationController*navController9=[[[UINavigationController alloc]initWithRootViewController:viewController9]autorelease];
navController1.title=@"About Us";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2,navController8, navController9, nil];
[navController1 release];
[navController2 release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I am creating app based on UTabbarController. I have creates that tab bar programmatically. Everything is running fine except I can not see the tabBatItem title. I have initialized everything properly, but when application launches all I can see is the first tabbar title. but if I select 2nd tabbaritem or so on I can see their names. I don't know whats going wrong here. Here is my code. Please let me know if I made any mistake.
Thanks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
HomeViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController*navController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navController1.title=@"Home";
[viewController1 release];
TrainerTableViewController *viewController2 = [[TrainerTableViewController alloc] initWithNibName:@"TrainerTableViewController" bundle:nil];
UINavigationController*navController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navController1.title=@"Trainer";
[viewController2 release];
SettingsTableViewController *viewController8 = [[[SettingsTableViewController alloc] initWithNibName:@"SettingsTableViewController" bundle:nil] autorelease];
UINavigationController*navController8=[[[UINavigationController alloc]initWithRootViewController:viewController8]autorelease];
navController1.title=@"Settings";
AboutUsViewController *viewController9 = [[[AboutUsViewController alloc] initWithNibName:@"AboutUsViewController" bundle:nil] autorelease];
UINavigationController*navController9=[[[UINavigationController alloc]initWithRootViewController:viewController9]autorelease];
navController1.title=@"About Us";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2,navController8, navController9, nil];
[navController1 release];
[navController2 release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过在每个选项卡栏项目的视图控制器的 .m 文件中包含以下代码来完成此操作。该代码还包括如何更改选项卡栏上的图像。
You can do this by including the code below inside the .m file of the view controller for each tab bar item. The code also includes how to change the image on the tab bar.
解决此问题的最佳方法是设置 viewController 的标题而不是 navigationController 的标题,
对所有选项卡执行此操作。
The best way to solve this problem is to set title of viewController not of navigationController
Do this for all tabs.
您只需设置 navController1 的标题。对于您创建的每个 nab 控制器,您需要为其设置标题。
试试这个:
改变:
navController1.title=@"训练员";
到
navController2.title=@"训练师";
改变:
navController1.title=@"设置";
到
navController8.title=@"设置";
改变:
navController1.title=@"关于我们";
到
navController9.title=@"关于我们";
也不是你没有释放 navController8 或 9 这会导致内存泄漏
You're only setting the title for navController1. For each nab controller you create you need to set the title for that one.
Try this:
CHANGE:
navController1.title=@"Trainer";
TO
navController2.title=@"Trainer";
CHANGE:
navController1.title=@"Settings";
TO
navController8.title=@"Settings";
CHANGE:
navController1.title=@"About Us";
TO
navController9.title=@"About Us";
Also not you are not releasing navController8 or 9 which will cause in a memory leak
您实际上是在设置导航控制器的标题。另外,请注意您的代码多次设置 navController1 的标题,而不是为其他代码设置它。
您可以通过为每个控制器设置
tabBarItem
来设置 tabBar 的标题。您可以选择子类化,或者只是将其包含在 AppDelegate 的
application:didFinishLaunchingWithOptions:
方法中。下面是一个示例:
这会将 tab1 的标题设置为“Home”,并将 tabBar 图标设置为名为“hometabicon.png”的文件。
您可以对其他每个选项卡重复相同的模式。
You are actually setting the title for the NavigationControllers. Also, watch that your code sets the title of navController1 multiple times, rather than setting it for the others.
You can set the titles for your tabBar by setting up
tabBarItem
s for each controller.You have the option of subclassing, or just including this in the
application:didFinishLaunchingWithOptions:
method in your AppDelegate.Here's an example:
This will set the title of tab1 to 'Home' and will set the tabBar icon to a file named 'hometabicon.png'.
You can repeat the same pattern for each of the other tabs.