iosobjective-cipadbackuisplitview

How to control the back button navigation in iPad (UISplitViewController)ios xcode


I have used the following code for deleting my login page from the navigationcontroller(viewcontrollers) so that it will not come into the view again when going back (back button).

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    NSMutableArray *VCs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    if([[VCs objectAtIndex:[VCs count] - 2] isKindOfClass:[loginViewController class]]&&(VCs.count>=4))
    {
        [VCs removeObjectAtIndex:[VCs count] - 2];
        [VCs removeObjectAtIndex:[VCs count] - 2];
        [self.navigationController setViewControllers: VCs];
    }
}

This works perfectly for iPhone. But for iPad, since we are using splitViewController, if we code like

NSMutableArray *VCs = [NSMutableArray arrayWithArray:self.splitViewController.viewControllers];

What we will be getting is an array of navigationControllers. Is there a genuine logic by which we can delete a particular viewcontroller from the splitviewcontroller?


Solution

  • Your split view controller, as you said, will return an array of nav controllers (depending on the project setup). Once you have a reference to those, you can manipulate them however you want.

    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *masterNavVC = (UINavigationController *)splitViewController.viewControllers.firstObject;
    UINavigationController *detailNavVC = (UINavigationController *)splitViewController.viewControllers.lastObject;
    
    //Now you have the master and detail navigation controllers, get your VC you need to manipulate
    NSMutableArray *masterVCs = masterNavVC.viewControllers;
    NSMutableArray *detailVCs = detailNavVC.viewControllers;
    
    //Remove the ones you need to - this example is arbitrary. Put your logic here
    if(masterVCs.count > 0 && [masterVCs[0] isKindOfClass:[LoginViewController class]])
    {
         //Remove or add
    }