Skip to content
CodeWorth
CodeWorth

Code worths when shared

Primary Navigation Menu
Menu
  • BLOG
    • DataBase
    • Programming
    • Mobile
    • Multimedia
    • OS Tips
    • Others
    • Web Development
  • Products
    • Utilities
    • Games
    • JsGenLib
      • PlugIns
      • Core Functions
      • Helper & ShortHands
  • About

IOS SplitViewController inside TabViewController

On June 28, 2012
In Mobile
Tagged ios, ipad, iphone
With 0 Comments
Sometimes you may want your splitviewController to be displayed within a TabBarController of an ipad application.

However we can go for either “Tab Based” application or “Split View Based” application but not a combination of both. But many IOS applications are tab based and it would be good if the existing tab based apps can be migrated to have ipad specific split views inside the tabs.

From Apple’s documentation this is a no no situation. But there is a way to achieve this easily (we did it for an application) and the following are the steps to achieve it.
  1. Create your own mySplitController extending UIViewController.
  2. Have an ivar and retainableProperty named leftController.
  3. Have an ivar and retainableProperty named rightController.
  4. Have a method as below method as below
    -(mySplitController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc
    {
    	if(self=[super init])
    	{
    		UINavigationController *lnc=[[UINavigationController alloc] initWithRootViewController:leftvc];
    		lnc.navigationBarHidden=NO;
    		self.leftController=lnc;
    		[lnc release];
    		
    		UINavigationController *rnc=[[UINavigationController alloc] initWithRootViewController:rightvc];
    		rnc.navigationBarHidden=NO;
    		self.rightController=rnc;
    		[rnc release];
    	}
    	return self;
    }
  5. Add a assigned property named detailNavigationController of type UINavigationController to your Root(/Left) VC.
    @interface RootVC : UIViewController 
    {
        UINavigationController *detailedNavigationController;
    }
    @property(nonatomic,assign) UINavigationController *detailedNavigationController;
    @end
    
    @implementation RootVC
    @synthesize detailedNavigationController;
    @end
    
  6. Now if RootVC is your root controller and DetailedVC is right/detail controller then the following code initialize it for you.
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        UITabBarController *tabBarController = [[UITabBarController alloc] init];
        //set tbconroller as root view controller of window
        [self.window setRootViewController:tabBarController];
        //window retains controller so we can release
        [tabBarController release];
        
        //create split view controller
        RootVC *rvc=[[RootVC alloc] init];
        DetailedVc *dvc=[[DetailedVc alloc] init];
        mySplitController *msc = [[mySplitController alloc] initwithLeftVC:rvc rightVC:dvc];
        
        //give access to detailed naviigation for rootvc
        rvc.detailedNavigationController=msc.rightController;
        
        //create a temporary VC to show in second tab
        UIViewController *vc2 = [[UIViewController alloc] init];
        
        //make an array containing these two view controllers
        NSArray *viewControllers = [NSArray arrayWithObjects:msc,vc2,nil];
        [tabBarController setViewControllers:viewControllers];
    
        //the views are retained their new owners, so we can release
        [msc release];
        [vc2 release];
    
        [self.window addSubview:tabBarController.view];
        self.window.rootViewController=tabBarController;
        [[self window] makeKeyAndVisible];
        
        return YES;
    }
    

Download Sample Code


On running sample app expected output will be similar to images below
  • Tab with a split view in it
  • Second Tab with a normal view in it
2012-06-28

Subscribe

Enter your email address to receive notifications about new posts via email.

Join 634 other subscribers

Google

Designed using Chromatic WordPress Theme. Powered by WordPress.