UISplitViewcontroller with master UINavigationController and detail UINavigationController

chris baxter picture chris baxter · Jan 7, 2011 · Viewed 9.8k times · Source

We have a certain behaviour we require in our UISplitViewController application. We have the following hierarchy of 3 views

  • FormOneViewController - TableViewController intialised in the MasterView
  • FormTwoViewcontroller - TableViewCotnroller initalised in detailView
  • FormThreeViewcontroller - not yet displayed.

When the user selects an item in FormTwoViewController we want FormThreeViewCOntroller to appear in the detailView, and FormTwoViewController (the current detail view) to become the masterView.

We also need to have a back button on the detail view to return up the stack of viewcontrollers. So when back is pressed, FormTwoViewController becomes the detailView and FormOneViewController becomes the master view again.

We have tried to implement this using the UISPlitViewcontroller and with a masterNavigationController and a detailNavigationController. We have the initial phase working where the views are displayed correctly when the app starts, we select the FormTwoViewController Item and it pushes FormThreeViewController onto the detailNavigationController and FormTwoViewController is pushed onto the masterNavigationController stack.

The problem we now have is twofold

  1. when the backbutton is pressed in the detail view controller it does nothing. it appears the handlers have got disconnected or something.
  2. We do not get a button in the portrait mode to display the masterview in a popover.

Has anyone got any examples of how to do this or any help wuld be appreciated.

Answer

Kshitiz Ghimire picture Kshitiz Ghimire · Jan 25, 2011

yes you can do that but u need to create seperate view controller for master and detail create new project as split view controller and remove split view from xib so that we are creating split view from code .

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after app launch.
    self.splitViewController =[[UISplitViewController alloc]init];
    self.rootViewController=[[RootViewController alloc]init];
    self.detailViewController=[[DetailViewController alloc]init];

    UINavigationController *rootNav=[[UINavigationController alloc]initWithRootViewController:rootViewController];
    UINavigationController *detailNav=[[UINavigationController alloc]initWithRootViewController:detailViewController];

    // Add the split view controller's view to the window and display.
    self.splitViewController.viewControllers=[NSArray arrayWithObjects:rootNav,detailNav,nil];
    self.splitViewController.delegate=detailViewController;
    [self.window addSubview:self.splitViewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

where rootviewcontroller is ur form one and detail view controller is ur form two.

in detail view controller ie ur form two create class variable SplitViewAppDelegate *appDelegate; //id ur app delegate variable set property and synthesize it.

then in ur form two

- (void)viewDidLoad {
self.appDelegate = (SplitViewAppDelegate *)[[UIApplication sharedApplication] delegate];
}

and finally while pushing ur form three

- (IBAction)pushViewController:(id)sender{
    NSLog(@"%@",self.appDelegate.splitViewController.viewControllers);
    RootLevel1 *rootLevel1 =[[RootLevel1 alloc]init];//create form 1 root vc and assign form 1 vc
    DetailLevel1 <UISplitViewControllerDelegate>*detailLevel1=[[DetailLevel1 alloc]init];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                                   initWithTitle: @"Home" 
                                   style:UIBarButtonItemStylePlain 
                                   target:self 
                                   action:@selector(home)];
    rootLevel1.navigationItem.leftBarButtonItem=backButton;
    [self.appDelegate.splitViewController viewWillDisappear:YES];
    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:0] pushViewController:rootLevel1 animated:YES];
    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] pushViewController:detailLevel1 animated:YES];
    self.appDelegate.splitViewController.delegate = detailLevel1;
    [self.appDelegate.splitViewController viewWillAppear:YES];

}

and for poping view controller

-(void)home {
    [self.splitViewController viewWillDisappear:YES];
    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:0]popViewControllerAnimated:YES];  
    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1]popViewControllerAnimated:YES];  
    UIViewController <UISplitViewControllerDelegate>*viewController=[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] visibleViewController];
    self.splitViewController.delegate=viewController;   
    [self.splitViewController viewWillAppear:YES];

}

set ur splitview delgeate accordingly.