0

我添加了一个标签栏控制器,并试图为项目添加标题和图像,但只显示图像。标题无处可寻。任何建议将不胜感激。

_tabbarController = [[UITabBarController alloc] init];
_showsController = [[showsController alloc] init];
_showsController.title = @"Test1";
_showsController.tabBarItem.image = [UIImage imageNamed:@"Glass.png"];
[_tabbarController setViewControllers: [NSArray arrayWithObjects: _showsController, nil]];

更新:在stackoverflow搜索了一段时间后,我终于通过添加以下代码找到了解决方案: *self.view = _tabbarController.view;*

我很抱歉没有提供更多信息(这不是 rootViewController 而是作为子视图添加的),并感谢您的帮助。

4

2 回答 2

0

您还需要在UITabBarItem对象上设置标题。

_showsController.tabBarItem.title = _showsController.title;

就个人而言,我更喜欢让 UIViewController 子类尽可能处理自己的标题。这使您的代码更加独立,并且并不总是强制 UIViewController 的创建者正确初始化它。

例如,在您的 UIViewController 子类中:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Test1"
                                                           image:[UIImage imageNamed:@"Glass.png"]
                                                             tag:1];
        [self setTabBarItem:item];
        [item release];
    }

    return self;
}
于 2012-01-03T15:25:44.690 回答
0

您需要设置UITabBarItem标题:

 _showsController.tabBarItem.title = @"Test1";

或者一次性设置它们:

_showsController.title = _showsController.tabBarItem.title = @"Test1";
于 2012-01-03T15:25:49.687 回答