在UITabBarController中更改标题

时间:2013-09-28 05:31:16

标签: ios objective-c uitabcontroller

我一直在尝试使用以下代码更改标签的标题,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.title = @"City Search";
        self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
    }

    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

这给了我一个iOS原生搜索图标和测试。但我想要做的是添加我自己的标题,我尝试做以下,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.tabBarItem = [[UITabBarItem alloc] init]
        self.tabBarItem.image = myImage;
        self.tabBarItem.title = @"FooBar";
    }

    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

然而,这只是给我一个没有任何文字的空白标签,其他一切正常。你能帮忙吗? 我正在使用iOS 6

4 个答案:

答案 0 :(得分:2)

这是一个简单的解决方案,  您的图像必须是黑白的,尺寸合适,尺寸为30x30

    self.title=NSLocalizedString(@"foobar", nil);
    self.tabBarItem.image=[UIImage imageNamed:@"image.png"];

答案 1 :(得分:2)

使用以下代码:

myTabItemContainViewController *addVC = [[myTabItemContainViewController alloc] init]; /// initialize object of your viewController       
self.tabBarItem = [[UITabBarItem alloc] init]
self.tabBarItem.image = myImage;
self.tabBarItem.title = @"FooBar";
[addVC setTabBarItem: self.tabBarItem ];

您需要将tabBarItem添加到特定的viewController。

答案 2 :(得分:0)

这个UIViewController的UITabBarItem已经被创建并添加 - 所以不需要分配新的。

只需修改当前的UITabBarItem。

您还可以设置选定的图片,如下所示:

-(void)awakeFromNib
{
    self.title = @"City Search";
    [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"citySearch_selected"]withFinishedUnselectedImage:[UIImage imageNamed:@"citySearch_unselected"]];
}

答案 3 :(得分:0)

以下实际工作:)但是我注意到如果图像文件不存在,这将无法正常工作......

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (self) {

        self.title = @"City Search";
        [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"about_tap_icon.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"about_tap_icon.png"]];
        [self.tabBarItem setBadgeValue:@"about"];
        [self.tabBarItem setTitle:@"hello"];
    }

    return self;
}