35

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

user278859
  • 10,379
  • 12
  • 51
  • 74

5 Answers5

90

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.

Community
  • 1
  • 1
Cowirrie
  • 7,218
  • 1
  • 29
  • 42
  • 3
    Wow, this was not obvious to me. I spent sometime trying to figure this out. Thanks. – rjgonzo Nov 27 '12 at 07:11
  • If this is happening upon app launch - isn't there still a flicker of the presenting view controller before the presented view controller is displayed? Any way around this? See http://stackoverflow.com/questions/14739461/how-do-you-transition-in-a-new-non-model-view-controller-without-using-uinavigat/14739540#14739540 for context. – Marplesoft Feb 07 '13 at 00:17
  • This is still the case for iOS 10 Xcode 8 Swift 2.3. Thanks! – justColbs Jan 09 '17 at 15:40
4

I have it resolved. I was trying to present the view from view did load of the main view controller. Not sure why it does not work there, but instead I am now setting a timer which calls a method to present the view controller after the main view loads and it works fine now using...

[self presentViewController:requestIPViewController animated:YES completion:nil]; 

Thanks to those who replied.

user278859
  • 10,379
  • 12
  • 51
  • 74
  • This method is only available in iOS 6 and beyond – Nate Symer Nov 25 '12 at 02:38
  • 4
    @NathanielSymer I don't think that's right. The docs say that presentViewController:animated:completion: is iOS5 and above. See http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/presentViewController:animated:completion: – Marplesoft Feb 06 '13 at 23:53
4

As @Dondragmer said, if you want to present your viewController in root view's viewDidLoad, it will fail.Once your viewController is ready for that, you can present your new viewController. So, you can do that in

- (void)viewDidLayoutSubviews {
    //present here
}
SeanChense
  • 846
  • 8
  • 14
  • 2
    ^ depends how many times you want to present it :) Da debugger's break point showed this getting called a couple of times in quick succession. – John Erck Oct 20 '17 at 21:49
0

I encountered the same problem. But my situation is the presentViewController is called after the dismissViewControllerAnimated for another ViewController. My solution is to move the presentViewController to completion block of dismissViewControllerAnimated.

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
-1

Present a modalViewController:

For the benefit of all starting programmers, type it instead of copy paste.

myVC *viewController = [[myVC alloc]initWithNibName:@"myVC" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];

It looks like you were trying to present a nav controller as a view controller in the first sample, then you were using the wrong method in the second one.

Nate Symer
  • 2,185
  • 1
  • 20
  • 27
  • Actually you are correct with regard to the presentModalViewController. I copied it wrong from my working code. I was, however, really wanting to get the presentViewController working as presentModalViewController has be deprecated. I did get it working... See my answer below. – user278859 Apr 14 '12 at 04:46
  • 3
    For future reference, don't do copy/paste programming. – Nate Symer Nov 25 '12 at 02:38