天天看點

iPhone SDK開發基礎之使用UITabBarController組織和管理UIView

iPhone SDK開發基礎之

使用UITabBarController組織和管理UIView

當你的程式分為幾個相對比較獨立的部分時,就比較适合使用UITabBarController來組織使用者界面,如圖3-26所示。

iPhone SDK開發基礎之使用UITabBarController組織和管理UIView

在螢幕的下方包含UITabBarController的三個按鈕,使用者單擊不同的按鈕即可以進入不同的界面,每個界面相對來說在整個系統中比較獨立,也就是程式分成了三個相對比較獨立的不同部分,在每個相對獨立的部分你也可以使用UINavigationController等容器類組織你的界面。這樣組織使程式邏輯非常清晰,當然你也可以組織很多個Tab而不隻是三個,以下代碼示範如何建立UITabBarController對象,并為其添加多個Tab。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {   

    // Override point for customization after application launch.

 //Create the navigation Controller

 UINavigationController *localNavigationController;

 //Create UINavigationController

 tabBarController = [[UITabBarController alloc] init];

    tabBarController.delegate = self;

 // Create the array that will contain all the View controlelr

 NSMutableArray *localControllersArray = [[NSMutableArray alloc] init WithCapacity:3];

 // Create the view controller attached to the first item in the TabBar

 aViewController *firstViewController;

 firstViewController = [aViewController alloc];

 localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:firstViewController];

 localNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

 [localNavigationController.tabBarItem initWithTitle:@"Outlines"

 image:[UIImage imageNamed:@"webcast.png"] tag:1];

 firstViewController.navigationItem.title = @"Outlines";

 [localControllersArray addObject:localNavigationController];

 [localNavigationController release];

 [firstViewController release];

 // Create the view controller attached to the second item in the TabBar

 anotherViewController *secondViewController;

 secondViewController = [[anotherViewController alloc] initWithStyle: UITableViewStyleGrouped ];

 localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:secondViewController];

 [localNavigationController.tabBarItem initWithTitle:@"Q & A"

   image:[UIImage imageNamed:@"book.png"] tag:2];

 [email protected]"Q & A";

 [localControllersArray addObject:localNavigationController];

 [localNavigationController release];

 [secondViewController release];

 miscViewController *thirdViewController;

 thirdViewController = [[miscViewController alloc] initWithStyle:UITable ViewStyleGrouped ];

 localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:thirdViewController];

 [localNavigationController.tabBarItem initWithTitle:@"Misc"

   image:[UIImage imageNamed:@"favorites.png"] tag:3];

 [email protected]"Misc";

 [localControllersArray addObject:localNavigationController];

 [localNavigationController release];

 [thirdViewController release];

 // load up our tab bar controller with the view controllers

 tabBarController.viewControllers = localControllersArray;

 // release the array because the tab bar controller now has it

 [localControllersArray release];

 // add the tabBarController as a subview in the window

 [window addSubview:tabBarController.view];

 // need this last line to display the window (and tab bar controller)

 [window makeKeyAndVisible];

    return YES;

}

捕獲Tab切換事件,擷取目前活動的Tab索引和UIViewController對象,代碼如下。

- (void)tabBarController:(UITabBarController *)barController didSelectView Controller:(UIViewController *)viewController{

 NSLog(@"currentController index:%d",viewController, tabBarController.selectedIndex);

 UIViewController  *currentController = tabBarController.selectedView Controller;

 NSLog(@"currentController: %@",currentController);

}

切換不同的Tab時,隻需要設定UITabBarController的selectedIndex屬性即可,代碼如下。

tabBarController.selectedIndex = 2;

本節相關的完整Xcode工程源代碼檔案請參考本書附帶的CD光牒中的Lessons2執行個體。

本文節選自《iOS軟體開發揭密:iPhone&iPad企業應用和遊戲開發》一書。

《iOS軟體開發揭密:iPhone&iPad企業應用和遊戲開發》一書已由電子工業出版社正式出版,本書由虞斌著。

iPhone SDK開發基礎之使用UITabBarController組織和管理UIView

互動出版網:http://product.china-pub.com/198191

繼續閱讀