简体   繁体   中英

display a SplitViewController from UIViewController in Objective-C

I am developing a ViewController (login app) with a single button, when I press this button I want to appear my UISplitView like this:

- (IBAction)loadSplitViewController:(id)sender {

    [self showSplitViewController];
}

and the code developed for the creation of my splitViewController is this:

-(void)showSplitViewController{

    UIStoryboard *mainStoryboard                    = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle: nil];

    LeftViewController      *leftViewController     = [mainStoryboard instantiateViewControllerWithIdentifier:@"LeftViewController"];
    RightViewController     *rightViewController    = [mainStoryboard instantiateViewControllerWithIdentifier:@"RightViewController"];

    UINavigationController  *leftNavController      = [[UINavigationController alloc] initWithRootViewController:leftViewController];
    UINavigationController  *rightNavController     = [[UINavigationController alloc] initWithRootViewController:rightViewController];

    UISplitViewController   *splitViewController    = [[UISplitViewController alloc] init];
    splitViewController.viewControllers             = [NSArray arrayWithObjects:leftNavController, rightNavController, nil];

    leftViewController.delegate     = rightViewController;
    splitViewController.delegate    = rightViewController;

    [self presentViewController:splitViewController animated:YES completion:^{}];
}

the thing is... if I use for display my splitViewController this line:

[self presentViewController:splitViewController animated:YES completion:^{}];

throws me an error

I also tried with

[self.view addSubview:splitViewController.view];

but this way my splitViewController never rotates, and delegates doesn't work as well... and I don't want my splitViewController to be a subview of my viewController, I want it to appear more like an independient modalView

any help I'll appreciate

thanks in advance

Split view controllers really should be the root view controller of the window (in fact Apple says it has to be, though there seem to be some examples where this isn't true). Instead of presenting it, you could just switch the window's root view controller to be the split view controller.

self.view.window.rootViewController = splitViewController;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM