The simplest way to make Split Controller in Tab Bar Controller

Discussion in 'iOS Programming' started by mikezang, Oct 17, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    Here is the simplest way to make Split Controller in Ta Bar Controller.

    1. Use Template SplitViewController to Make a project named TemplateSplit and compile it.

    2. Use Template TabBarController to make a project named SplitInTabBar and compile it.

    3. Add files RootViewController.*, DetailViewController.*, DetailView.xib of project TemplateSplit to project SPlitInTabBar.

    4. Add a new tab in project SplitInTabBar.

    5. Add code as below to file SplitInTabBarAppDelegate.h.
    Code:
    #import <UIKit/UIKit.h>
    
    [B]@class RootViewController;
    @class DetailViewController;[/B]
    
    @interface SplitInTabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
        UIWindow *window;
        UITabBarController *tabBarController;
    [B]    UISplitViewController *splitViewController;
        RootViewController *rootViewController;
        DetailViewController *detailViewController;[/B]
    }
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
    [B]@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
    @property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
    @property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;[/B]
    
    @end
    
    6。Add code as below to file SplitInTabBarAppDelegate.m.
    Code:
    #import "SplitInTabBarAppDelegate.h"
    [B]#import "RootViewController.h"
    #import "DetailViewController.h"[/B]
    
    @implementation SplitInTabBarAppDelegate
    
    @synthesize window;
    @synthesize tabBarController;
    [B]@synthesize splitViewController;
    @synthesize rootViewController;
    @synthesize detailViewController;[/B]
    
    #pragma mark -
    #pragma mark Application lifecycle
    
    [B]-(void) makeSplitViewController {
        NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
        int index = 0; 
        for (UIViewController *controller in tabBarController.viewControllers) {
            if (index == 2) {
                detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
                rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
                rootViewController.detailViewController = detailViewController;
                rootViewController.navigationItem.title = @"List";
                UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
                splitViewController = [[UISplitViewController alloc] init];
                splitViewController.tabBarItem = controller.tabBarItem;
                splitViewController.viewControllers = [NSArray arrayWithObjects:nav, detailViewController, nil];
                splitViewController.delegate = detailViewController;
                [controllers replaceObjectAtIndex:index withObject:splitViewController];
            }
            index++;
        }
        tabBarController.viewControllers = controllers;
    }[/B]
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        // Override point for customization after app launch.
        [B][self makeSplitViewController];[/B]
        
        // Add the tab bar controller's current view as a subview of the window
        [window addSubview:tabBarController.view];
        [window makeKeyAndVisible];
        
        return YES;
    }
    
     

    Attached Files:

  2. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #2
    7. Now compile project SplitInTabBar and you got Split Controller in Tab Bar Controller.
     

    Attached Files:

  3. thepost macrumors newbie

    Joined:
    Nov 18, 2010
    Location:
    Toronto
    #3
    That's great, do you know how to do it the other way around? I'm really struggling on how to have a UITabBarController inside a UISplitViewController!

    As in, the tab bar controller would make the view of the detail view controller...
     

Share This Page