gpt4 book ai didi

iphone - UIViewController 中的 ViewDidLoad 方法 - 何时调用它?

转载 作者:可可西里 更新时间:2023-11-01 03:30:52 26 4
gpt4 key购买 nike

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

@interface LaunchController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

然后,当一个按钮被点击时,我按下另一个 View Controller :

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

MainController 具有以下我使用的构造函数:

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

return self;
}

然后它有一个 viewDidLoad 方法,如下所示:

- (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

最佳答案

我认为是以下几点。当你在 UIViewController 中需要 view 的属性时,它会以惰性方式加载。

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

return _view;
}

view初始化后,会调用viewDidLoad通知UIViewController

关于iphone - UIViewController 中的 ViewDidLoad 方法 - 何时调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com