UIViewController中的ViewDidLoad方法-什么时候被调用? [英] ViewDidLoad method in UIViewController - when does it get called?

查看:56
本文介绍了UIViewController中的ViewDidLoad方法-什么时候被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为LaunchController的 UIViewController ,该应用程序在首次打开时在我的iPhone应用程序中启动:

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具有以下构造函数,供我使用:

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;
}

,然后具有如下viewDidLoad方法:

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");
}

程序运行时,我看到调用了 MainController 的构造函数(由于 NSLog 的输出),但是 viewDidLoad 从来没有被调用,即使我正在调用 presentModalViewController .为什么是这样?为什么不调用 viewDidLoad ?

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?

推荐答案

我认为这是以下内容.当您需要UIViewController中的 view 属性时,它将以惰性方式加载.

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;
} 

初始化 view 后,它将调用 viewDidLoad 通知 UIViewController .

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

这篇关于UIViewController中的ViewDidLoad方法-什么时候被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆