Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Application tried to present Split View Controllers modally"

I am still trying to find a solution to what is theoretically a very simple task and that is going from a UIViewController to a Split View Controller. (Why have apple made this so difficult).

I am at a stage where I now put the SplitViewController into its own storyboard. And when user selects a button on the single UIView Controller I call the following code:

UISplitViewController *splitVC = [[UIStoryboard storyboardWithName:@"SplitStoryBoard" bundle:nil] instantiateViewController];
    [self presentViewController:splitVC animated:YES completion:nil];

So I want to load the storyboard with the splitview controller. But this also crashes with "Application tried to present Split View Controllers modally"

I did not specify any "modal" action in the code.

Is there any solution for how to do this ?

like image 331
drlobo Avatar asked Dec 02 '13 11:12

drlobo


3 Answers

The UISplitViewController has to be always the root view controller, and cannot be presented modally. See Apple docs, that says:

A split view controller must always be the root of any interface you create. In other words, you must always install the view from a UISplitViewController object as the root view of your application’s window. The panes of your split view interface may then contain navigation controllers, tab bar controllers, or any other type of view controller you need to implement your interface. Split view controllers cannot be presented modally.

like image 51
neutrino Avatar answered Oct 13 '22 01:10

neutrino


You can use a splitViewController as a view to a TabViewController, however a splitViewController cannot be a modal controller in the traditional sense. To transition to a SplitViewController from a UIViewController, use code like this:

- (IBAction)setupTapped:(id)sender {
    static NSString *tabViewControllerIdentifier = @"SetupViewController";  


    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:tabViewControllerIdentifier];
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    UIViewController *currentController = app.window.rootViewController;
    app.window.rootViewController = controller;
    app.window.rootViewController = currentController;

    [UIView transitionWithView:self.navigationController.view.window
                      duration:0.75
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        app.window.rootViewController = controller;
                    }
                    completion:nil];
}

That will transition between controllers. You can change the animation type to suit your purpose. This is set with the options parameter; currently showing UIViewAnimationOptionTransitionFlipFromRight.

like image 45
Aaron Bratcher Avatar answered Oct 12 '22 23:10

Aaron Bratcher


This one is quite old question. However I got it worked using below code in my app and I think it is better solution.

[self addChildViewController:<# Your Split VC #>];
[<# Your Split VC #>.view willMoveToSuperview:self.view];
<# Your Split VC #>.view.frame = self.view.frame;
[self.view addSubview:<# Your Split VC #>.view];
[<# Your Split VC #>.view didMoveToSuperview];

The idea is to take a view controller (Parent) and add split view controller as its child. Perform model segue to parent as you do for any other controller.

Well I am not sure but from iOS8 SplitViewController got lot better and it should be ok to use it somewhere else than root view controller.

Cheers!

like image 27
Pradip Vaghasiya Avatar answered Oct 13 '22 01:10

Pradip Vaghasiya