新專案開始實行,View必須使用Navigation Controller和Tab Bar Controller,這裡的關鍵就是先產生Navigation Controller,接著塞到Tab Bar Controller。
建立新專案時,在iOS的Application選擇Tabbed Application,它預設有兩個Tab頁面,在此我改成三個~
在AppDelegate.m中,程式碼改成這樣子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[PLWorkViewController alloc] init];
UINavigationController *navigationViewController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[PLProductViewController alloc] init];
UINavigationController *navigationViewController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UIViewController *viewController3 = [[PLAccountViewController alloc] init];
UINavigationController *navigationViewController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navigationViewController1, navigationViewController2, navigationViewController3];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
示範從作品View的按鈕轉場到產品View~經過測試,此三個View會在這一刻產生好,之後去點選Tab可以馬上做反應!
點擊「新作品製作」後,便會跑到下一個View,可見到Navigation Bar返回鈕出現「作品」。只是這裡Tab Bar並不是選擇「產品」項目。
想設定Navigation Bar和Tab Bar的Title可以在各個ViewController這麼設定~
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"產品", @"Product");
self.tabBarItem.image = [UIImage imageNamed:@"icon_product.png"];
}
return self;
}
參考:Tab bar controller inside a navigation controller, how to push new viewcontrollers to the tabcontroller?、產生含有 Navigation controller 的 Tab bar controller。


隨意留個言吧:)~