Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewDidLoad method in UIViewController - when does it get called?

I have a UIViewController called LaunchController that is launched in my iPhone app when the app first opens:

@interface LaunchController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

Then, when a button is clicked, I push another view controller:

 MainController *c = [[MainController alloc] initWithImage:image];
 [self presentModalViewController:c animated:NO];

MainController has the following constructor, which I use:

- (id)initWithImage:(UIImage *)img
{
    self = [super init];
    if (self) {
        image = img;
        NSLog(@"inited the image");
    }

    return self;
}

and then it has a viewDidLoad method as follows:

- (void)viewDidLoad
{
    NSLog(@"calling view did load");
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
    NSLog(@"displaying main controller");
}

When the program runs, I see that the constructor for MainController is called (due to the output of NSLog), however viewDidLoad never gets called, even though I am calling presentModalViewController. Why is this? Why isn't viewDidLoad being called?

like image 323
CodeGuy Avatar asked Jan 16 '12 06:01

CodeGuy


People also ask

How often is viewDidLoad called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

Does viewDidLoad get called before viewWillAppear?

Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called. This gives you a chance to do any last-minute view setup, kick off a network request (in another class, of course), or refresh the screen.

Is viewDidLoad called once?

viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.

Which method is called before viewDidLoad?

When launching an app in landscape, viewWillTransition(to:with:) is called before viewDidLoad().


2 Answers

I think it is something as followings. When you need the property of view inside UIViewController, it will be loaded with lazy manner.

- (UIView *)view 
{
   if (_view == nil) {
      [self loadView]; //< or, the view is loaded from xib, or something else.
      [self viewDidLoad];
   }

   return _view;
} 

After the view initialized, it will call viewDidLoad to inform the UIViewController.

like image 122
AechoLiu Avatar answered Dec 14 '22 23:12

AechoLiu


You aren't loading your view controller from a xib file, and from comments you don't have anything in loadView (which is where you would create your view controller's view if you were not using a xib file).

Therefore, your view isn't being loaded, so viewDidLoad is never called.

Typically you would use initWithNibName: to initialise a new view controller, and then set the image after it (so expose the image as a property).

viewDidLoad will be called as soon as your controller's view property is accessed, that is when you display it for the first time or request it (e.g. have some code that calls c.view.

like image 36
jrturton Avatar answered Dec 14 '22 23:12

jrturton