上次說了如何通過代碼創建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