UIViewController 中的 ViewDidLoad 方法 - 什么时候调用它?
我有一个名为 LaunchController 的 UIViewController,当应用程序首次打开时,它会在我的 iPhone 应用程序中启动:
@interface LaunchController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
然后,当单击按钮时,我会推送另一个视图控制器:
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
没有被调用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为是以下内容。当你需要UIViewController中
view
的属性时,它会以惰性方式加载。view
初始化后,会调用viewDidLoad
来通知UIViewController
。I think it is something as followings. When you need the property of
view
inside UIViewController, it will be loaded with lazy manner.After the
view
initialized, it will callviewDidLoad
to inform theUIViewController
.您没有从 xib 文件加载视图控制器,并且从注释中看,您在 loadView 中没有任何内容(如果您不使用 xib 文件,您将在其中创建视图控制器的视图)。
因此,您的视图没有被加载,因此 viewDidLoad 永远不会被调用。
通常,您会使用 initWithNibName: 初始化新的视图控制器,然后在其后面设置图像(因此将图像公开为属性)。
一旦访问控制器的视图属性,即当您第一次显示它或请求它时(例如,有一些调用
c.view
的代码),viewDidLoad就会被调用。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
.viewDidLoad 没有被调用的原因是因为您没有加载视图。
在你的 init 方法中:
意味着你只是从头开始创建一个裸视图。不从笔尖加载一个。
试试这个:
如果你有一个与视图控制器类同名的 xib 或 nib 文件,它应该找到 if。否则,您可以只给出一个有效的 nibName 。
更新:
如果您不使用 nib 文件,那么适当的方法不是 viewDidLoad。您必须实现
loadView
而不是viewDidLoad
。在您的具体情况下,只需将当前 viewDidLoad 中的所有内容放入 loadView 中即可。
The reason viewDidLoad is not being called is because you aren't loading a view.
In your init method:
means that you are just creating a naked view from scratch. not loading one from a nib.
try this instead:
If you have a xib or nib file with the same name as the view controller class it should find if. Otherwise, you can just give a nibName that works.
UPDATE:
If you are not using nib files, then the appropriate method is NOT viewDidLoad. You have to implement
loadView
instead ofviewDidLoad
.In your specific case, just put everything that is currently in viewDidLoad into loadView.