Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
931
38
Tokyo, Japan
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;
}
 

Attachments

  • img_1501553_59946739_0.jpeg
    img_1501553_59946739_0.jpeg
    93.7 KB · Views: 191
  • img_1501553_59946739_1.jpeg
    img_1501553_59946739_1.jpeg
    90.3 KB · Views: 144
  • img_1501553_59946739_2.jpeg
    img_1501553_59946739_2.jpeg
    219.5 KB · Views: 183
  • img_1501553_59946739_3.jpeg
    img_1501553_59946739_3.jpeg
    56.6 KB · Views: 144
7. Now compile project SplitInTabBar and you got Split Controller in Tab Bar Controller.
 

Attachments

  • img_1501553_59946739_4.jpeg
    img_1501553_59946739_4.jpeg
    46.8 KB · Views: 161
  • img_1501553_59946739_5.jpeg
    img_1501553_59946739_5.jpeg
    44.6 KB · Views: 170
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.