UINavigationController和UITabBarController合用

開發環境:Xcode4.5


很多時候我們創建一個基於UITabBarController的application以後還希望能夠在每個tab view都可以實現導航控制,即添加一個UINavigationController來實現tabview內部的view之間的切換,這即是本文所要介紹的。


一、創建一個 Tabbed Application.默認創建的是帶有兩個Tab的工程。

二、在AppDelegate.h裏面添加

@property (strong, nonatomic) UINavigationController *NaviView1Controller;
@property (strong, nonatomic) UINavigationController *NaviView2Controller;

三、修改AppDelegate.m文件的didFinishLaunchingWithOptions函數,在這裏我們設置相應的UINavigationController.

修改前:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
修改後:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    viewController1.title = @"View1";
    self.NaviView1Controller = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];

    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
    viewController2.title = @"View2";
    self.NaviView2Controller = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    
    self.tabBarController.viewControllers = @[self.NaviView1Controller, self.NaviView2Controller];
    
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

運行一下程序,可以看到每個Tab view都自動多了一個NavigationBar在頂部。


三、添加兩個新的View測試NavigationController。

1. 創建Test1ViewController類,以及對應的xib文件。

2. 創建Test2ViewController類,以及對應的xib文件。

3. 如果你的iPhone iOS版本低於6.0,在xib的設置裏需要勾掉autolayout選項。


四、在Tab view 1的xib文件裏添加一個按鈕,並設置相應的IBOutlet, IBAction.如下圖:



五、編寫代碼邏輯。

在FirstViewController.m中。編寫函數gotoTest1View

- (IBAction)gotoTest1View:(id)sender {
    Test1ViewController *SRDVController = [[Test1ViewController alloc]init];
    [self.navigationController pushViewController:SRDVController animated:YES];
    [SRDVController release];
}
同理可以測試,Test2ViewController.不再贅述。


六、代碼下載:http://download.csdn.net/detail/lovefqing/4845092


若本文和代碼有任何錯誤,歡迎拍磚指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章