同时创建navigationController和tabBarController(转)
上次说了如何通过代码创建TabBar,但是在这一过程中我遇到一个困难,就是又要创建navigationBarController又要创建 TabBarController,所以这就比较纠结了。不过经过一番Google之后,还是解决了这个问题,所以在这也就写一下,当做自己总结了。如果 有错误还请提出:
第一种方式是在AppDelegate中将tabBarController作为subView,然后再在tabBarController的基础上增加navigationController,代码如下:
在applicationDidFinishLauchingWithOptions中加入以下代码:
-(BOOL)application:(UIApplication *)applicationDidFinishLauchingWithOptions:(NSDictionary *)lauchingOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController *tabBarController = [[[UITabBarController alloc] init]autorelease];
FirstViewController *firstController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *firstNavigation = [[[UINavigationController alloc] initWithRootViewController:firstController]autorelease];
[firstController release];
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *secondNavigation = [[[UINavigationController alloc] initWithRootViewController:secondController]autorelease];
[secondController release];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavigation,secondNavigation,nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
这样之后分别在FirstViewController.m和SecondViewController.m里面加入如下代码:
-(id)init
{
self = [super initWithNibName:nil bundle:nil];
if(self)
{
UITabBarItem *tabBarItem = [self tabBarItem];
[tabBarItem setTitle] = @"xxx";
[tabBarItem setImage] = [UIImage imageNamed:@"xxxxx"];
}
return self;
}
-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *bundle)
{
return [self init];
}
这样的话,就创建了既有navigationBar又有TabBar的应用了,而且这种方法是tabBar在每个界面都是存在的。
第二种方法,仅仅是在一个页面上显示tabBar,这种方法不像上一种,需要将两个tabBar的界面放在一个根视图(假设为InitialViewController),代码如下:
在applicationDidFinishLauchingWithOptions中加入以下代码:
-(BOOL)application:(UIApplication *)applicationDidFinishLauchingWithOptions:(NSDictionary *)lauchingOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
InitialViewController *initial = [[[InitialViewController alloc]init]autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc]initWithRootViewController:initial]autorelease];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
}
然后在InitialViewController.m中import需要加载到tabBar的ViewController,加上如下方法:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nil bundle:nil])
{
FirstViewController *firstController = [[FirstViewController alloc]init];
UIImage *image1 = [UIImage imageNamed:@"tabBar1.png"];
firstController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"xxxx" image:image1 tag:0]autorelease];
SecondViewController *secondController = [[SecondViewController alloc] init];
UIImage *image2 = [UIImage imageNamed:@"tabBar2.png"];
secondController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"xxxx" image:image2 tag:0]autorelease];
// 组装TabBar
self.viewControllers = [NSArray arrayWithObjects:firstController,secondController, nil];
[firstController release];
[secondController release];
}
return self;
}
OK
花开花谢春不管,水暖水寒鱼自知.