simonbs
simonbs

Reputation: 8042

UISplitViewController won't present modal view controller

I need to present a modal view controller before showing a split view controller. I need this because the user will have to log in.

I have read answers on this forum suggesting that the modal view controller should be presented from the AppDelegate but when trying to do so, nothing happens.

I have set up my view controller in the same storyboard as the rest of my interface is in and I have given the view controller the identifier loginViewController. I am trying to show the view controller in the AppDelegate like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
        UINavigationController *navigationController = splitViewController.viewControllers.lastObject;
        splitViewController.delegate = (id) navigationController.topViewController;

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
        LoginViewController *lvc = (LoginViewController *) [storyboard instantiateViewControllerWithIdentifier:@"loginViewController"];
        lvc.modalPresentationStyle = UIModalPresentationFullScreen;
        [splitViewController presentModalViewController:lvc animated:YES];
    }

    [_window makeKeyAndVisible];

    return YES;
}

When I do so, nothing happens. No errors, no modal view controller, no nothing. The application just shows my split view controller.

Can anybody tell me how I can show a modal view controller before showing the split view controller?

Upvotes: 4

Views: 4017

Answers (2)

nyus2006
nyus2006

Reputation: 441

Instead of doing it in AppDelegate.m, do that in DetailViewController:

LogInViewController *logInVC = [[LogInViewController alloc] init];
[self presentModalViewController:logInVC animated:NO];

This works for me.

Upvotes: 1

Naveen Shan
Naveen Shan

Reputation: 9192

A viewcontroller will not allow to push/present on another viewcontroller unless and until the view is complete loading.

Simply saying we are not allow to call presentModalViewController/pushViewController in a viewcontroller viewDidLoad/viewWillAppear. we need to call this in viewDidAppear.

I had the same issue you said.

Some Solution I can say are,

Do the loading of LoginViewController after [self.window makeKeyAndVisible]; and in a performSelctor (may be with a delay). Move the code to display LoginViewController in SplitView's DetailView controller viewDidAppear.

thanks,

Naveen Shan

Upvotes: 6

Related Questions