0

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

- (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

4 回答 4

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。

于 2013-09-28T05:58:06.197 回答
2

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

    self.title=NSLocalizedString(@"foobar", nil);
    self.tabBarItem.image=[UIImage imageNamed:@"image.png"];
于 2013-09-28T05:39:15.863 回答
0

这个 UIViewController 的 UITabBarItem 已经被创建和添加了——所以没有必要分配一个新的。

只需修改当前的 UITabBarItem。

您还可以像这样设置选定的图像:

-(void)awakeFromNib
{
    self.title = @"City Search";
    [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"citySearch_selected"]withFinishedUnselectedImage:[UIImage imageNamed:@"citySearch_unselected"]];
}
于 2013-09-28T13:19:34.870 回答
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;
}
于 2013-09-28T22:32:58.930 回答