第一次海报。提前致谢。

我想在我的iPhone应用程序中使用UITabBar。我被认为无法使用UITabViewController,因为我会将其嵌套在UINavigationController中。我读到您可以执行此操作,但是您必须使用UITabBar而不是完整的控制器。我可以使用Interface Builder来完成这项工作,但是我有很多冗余的XIB,无论如何我还是愿意用代码来完成。我想知道魔术是如何工作的。

这是我的控制器的.m:

#import "DumbViewController.h"

@implementation DumbViewController

-(void) loadView {
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 30)];
    [label setText:@"hello world"];
    [view addSubview:label];
    [label release];

    UITabBar *tb = [[UITabBar alloc] initWithFrame:CGRectMake(0, [view frame].size.height - 64, [view frame].size.width, 64)];
    [tb setDelegate:self];
    [tb setItems:[NSArray arrayWithObject:[[[UITabBarItem alloc] initWithTitle:@"bob" image:[UIImage imageNamed:@"21-skull.png"] tag:0] autorelease]]];
    [view addSubview:tb];
    [tb release];

    [self setView:view];
    [view release];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSLog(@"selected item %@", item);
}

@end


运行该应用程序时,我在底部获得标签和标签栏。我得到了愚蠢的小骷髅图标,但是“ bob”从不显示。单击头骨可以按预期工作。

为什么不显示该标题?

谢谢!

最佳答案

尝试以下代码。

    UITabBarItem *someTabBarItem = [[UITabBarItem alloc] initWithTitle:@"bob" image:[UIImage imageNamed:@"21-skull.png"] tag:0];
    NSArray *tabBarItems = [[NSArray alloc] initWithObjects:someTabBarItem,nil];
    [tabBar setItems:tabBarItems animated:NO];
    [tabBar setSelectedItem:someTabBarItem];
    [tabBarItems release];
    [someTabBarItem release];

关于iphone - 为什么不显示UITabBarItems的标题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7266151/

10-09 02:16