UITabBarController 不显示 tabbarItems 名称

发布于 2024-12-11 22:06:47 字数 2025 浏览 0 评论 0原文

我正在创建基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妄断弥空 2024-12-18 22:06:48

您可以通过在每个选项卡栏项目的视图控制器的 .m 文件中包含以下代码来完成此操作。该代码还包括如何更改选项卡栏上的图像。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
        self.title = @"Apply Now";
        self.tabBarItem.image = [UIImage imageNamed:@"tbApplyNow.png"];
    }
    return self;
}

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.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
        self.title = @"Apply Now";
        self.tabBarItem.image = [UIImage imageNamed:@"tbApplyNow.png"];
    }
    return self;
}
爱已欠费 2024-12-18 22:06:48

解决此问题的最佳方法是设置 viewController 的标题而不是 navigationController 的标题,

viewController1.tabBarItem.title = @"CohesiveSelf";

对所有选项卡执行此操作。

The best way to solve this problem is to set title of viewController not of navigationController

viewController1.tabBarItem.title = @"CohesiveSelf";

Do this for all tabs.

静若繁花 2024-12-18 22:06:48

您只需设置 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

淡紫姑娘! 2024-12-18 22:06:48

您实际上是在设置导航控制器的标题。另外,请注意您的代码多次设置 navController1 的标题,而不是为其他代码设置它。

您可以通过为每个控制器设置 tabBarItem 来设置 tabBar 的标题。

您可以选择子类化,或者只是将其包含在 AppDelegate 的 application:didFinishLaunchingWithOptions: 方法中。

下面是一个示例:

UITabBarItem *tbi1 = [navController1 tabBarItem];
[tbi1 setTitle:@"Home"];

UIImage *i1 = [UIImage imageNamed:@"hometabicon.png"];
[tbi1 setImage:i1];

这会将 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 tabBarItems 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:

UITabBarItem *tbi1 = [navController1 tabBarItem];
[tbi1 setTitle:@"Home"];

UIImage *i1 = [UIImage imageNamed:@"hometabicon.png"];
[tbi1 setImage:i1];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文