rwyland
rwyland

Reputation: 1647

Issue Presenting View modally after a UISplitView loads

I'm new to UISplitView development, so I'm sure there is something obvious I'm doing wrong. I have a basic UISplitView iPad app that loads up with two UITableView controllers when the app launches. This works just fine.

What I am trying to do is immediately upon launch, presenting an "authentication" view modally so that a user will need to login before continuing. Here is the code I have so far which compiles and works without breaking, but the view is not showing.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
  UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];

  DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];

  self.splitViewController = [[UISplitViewController alloc] init];
  self.splitViewController.delegate = detailViewController;
  self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];

  masterViewController.detailViewController = detailViewController;
  masterViewController.managedObjectContext = self.managedObjectContext;

  self.window.rootViewController = self.splitViewController;
  [self presentAuthenticate];
  [self.window makeKeyAndVisible];

  applicationDidLaunch = YES;
  return applicationDidLaunch;
}

- (void) presentAuthenticate {
  AuthenticateViewController *loginController = [[AuthenticateViewController alloc] initWithNibName:@"AuthenticateViewController" bundle:nil];
  [loginController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
  [loginController setModalPresentationStyle:UIModalPresentationFormSheet];

  if ([self.splitViewController respondsToSelector:@selector(presentViewController:animated:completion:)]) {
      [self.splitViewController presentViewController:loginController animated:NO completion:nil];
  } else {
      [self.splitViewController presentModalViewController:loginController animated:NO]; //iOS 4 works fine with or without animation   
  } 
}

I defined the AuthenticateViewController as a View with a few textfields in it and have it wired to the File's Owners view.

Thanks ahead of time!

Upvotes: 2

Views: 657

Answers (1)

Naveen Shan
Naveen Shan

Reputation: 9192

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

Simple 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,

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

thanks,

Naveen Shan

Upvotes: 2

Related Questions