2010-03-15 78 views

回答

25
yourViewController.tabBarItem = [[UITabBarItem alloc] 
initWithTitle:NSLocalizedString(@"Name", @"Name") 
image:[UIImage imageNamed:@"tab_ yourViewController.png"] 
tag:3]; 

的viewControllers被添加到标签栏,所以在标签栏变得可见的形象和名称应设置(如果的appDelegate他们那里例如应用程序启动)。之后,您可以使用上面的代码来更改viewController中的loadView或viewDidAppear的图标和文本。

+1

这完美地工作viewcontrollers。谢谢! – 2010-03-16 01:16:40

+0

这在iOS 4.x中不起作用,并且您忘记释放内存 – Gargo 2012-09-05 09:44:45

1

是的。您的UITabBar有一个名为items的属性,该属性是每个标签栏项目的UITabBarItem s的数组。您可以使用–initWithTitle:image:tag:构造函数创建一个UITabBarItem以使用您自己的图像,或者使用–initWithTabBarSystemItem:tag:构造函数来使用系统图像。

1

您也可以在AppDelegate中通过声明UITabBarController iVar并将其指向应用程序tabBarController来执行此操作。您可以使用items阵列访问各个标题。和setTitle

@synthesize tabBarController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    self.tabBarController = (UITabBarController*)self.window.rootViewController; 
    NSArray* items = [self.tabBarController.tabBar items]; 
    [[items objectAtIndex:0] setTitle:@"Home"]; 
    [[items objectAtIndex:1] setTitle:@"Cool"]; 
    [[items objectAtIndex:2] setTitle:@"Stuff"]; 
    [[items objectAtIndex:3] setTitle:@"Settings"]; 
    return YES; 
} 
0

正确的方法是:在viewDidLoad中

[self.tabBarItem setImage:[UIImage imageNamed:@"<Image Name>"]]; 

以下行添加这被设置里面的UITabBarController