更改WindowBased应用程序中的TabBarItem文本
我创建基于窗口的应用程序(称为TabBar)。 在TabBarAppDelegate中,我创建了两个UIViewControllers和一个UITabBarController。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIViewController *vc1 = [[UIViewController alloc]
initWithNibName:@"MyFirstViewController" bundle:nil];
UIViewController *vc2 = [[UIViewController alloc]
initWithNibName:@"MySecondViewController" bundle:nil];
NSArray *contr = [NSArray arrayWithObjects:vc1, vc2, nil];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = contr;
[self.window addSubview:tbc.view];
[self.window makeKeyAndVisible];
[vc1 release]; [vc2 release];
return YES;
}在MyFirstViewController.xib中,我创建了uilabel(使用IB进行测试,视图已加载)以及MySecondViewController.xib。
而应用程序的构建和运行成功,但uitabbaritem - 没有图像和标题。 我试图添加MyFirstViewController.m这行代码来设置MyFirstViewController的标题和图像
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UITabBarItem *item = [[UITabBarItem alloc]
initWithTitle:@"first" image:[UIImage imageNamed:@"first.png"] tag:0];
self.tabBarItem = item;
[item release];
}
return self;
}
但第一个tabbaritem不会改变。 请帮帮我。 我不明白我做错了什么。
答案是
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
MyFirstViewController *vc1 = [[MyFirstViewController alloc]
initWithNibName:@"MyFirstViewController" bundle:nil];
MySecondViewController *vc2 = [[MySecondViewController alloc]
initWithNibName:@"MySecondViewController" bundle:nil];
NSArray *contr = [NSArray arrayWithObjects:vc1, vc2, nil];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = contr;
[self.window addSubview:tbc.view];
[self.window makeKeyAndVisible];
[vc1 release]; [vc2 release];
return YES;
}
链接地址: http://www.djcxy.com/p/87715.html
