我创建基于窗口的应用程序(称之为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并没有改变。请帮帮我。我不明白我做错了什么。
答案 0 :(得分:0)
答案是
- (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; }