Unable to get presentViewController to work

user278859 picture user278859 · Apr 13, 2012 · Viewed 60.4k times · Source

I copied a working viewcontroller class from another project into a new project. I can't get the view to load in the new project. In the old project I used presentModalViewController. In the new I cannot get the view to load using either presentModalViewController or presentViewController

I am trying to load the present the view from my main view controller.

Here is what my main view controller interface looks like...

//  ViewController.h
#import <UIKit/UIKit.h>
#import "RequestDialogViewController.h"

@interface ViewController : UIViewController <RequestDialogViewControllerDelegate> {

}

- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)response;

I am using presentModalViewController like this...

RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController"  bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:requestIPViewController];
[self presentModalViewController:navigationController animated:YES];

and presentViewController like this...

RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController"  bundle:nil];    
[self presentViewController:requestIPViewController animated:YES completion:nil];

What am I missing in the new project? The init method fires, but viewDidLoad does not and nothing is displayed.

Thanks

Answer

Dondragmer picture Dondragmer · Apr 14, 2012

If ViewController is the root view controller, it can't present a modal view controller from within its own viewDidLoad, because at that point it doesn't have information like the screen size.

If other view controllers have already displayed, this will work. If the root view controller is a UINavigationController, you will see a view sliding in from the right while the modal view slides up from the bottom.

Anyway, for your ViewController, the soonest you could present it is after it has become visible. Using a timer for this is unreliable; older and slower devices have dramatically longer load times.

For more reliability, implement viewDidAppear: for ViewController. Do still use your timer system to add an additional delay; a fraction of a second should be sufficient. Although presenting the modal view controller from within viewDidAppear worked for me in the iOS 5.1 simulator, Presenting a modal view controller when loading another ViewController says it sometimes doesn't happen.