display a SplitViewController from UIViewController in Objective-C

為{幸葍}努か 提交于 2019-12-13 02:10:33

问题


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


回答1:


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;


来源:https://stackoverflow.com/questions/18906738/display-a-splitviewcontroller-from-uiviewcontroller-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!