UIViewController 中的 ViewDidLoad 方法 - 什么时候调用它?

发布于 2024-12-26 22:58:26 字数 1125 浏览 2 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷炫老祖宗 2025-01-02 22:58:26

我认为是以下内容。当你需要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

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.

憧憬巴黎街头的黎明 2025-01-02 22:58:26

您没有从 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.

葬心 2025-01-02 22:58:26

viewDidLoad 没有被调用的原因是因为您没有加载视图。

在你的 init 方法中:

 self = [super init];

意味着你只是从头开始创建一个裸视图。不从笔尖加载一个。

试试这个:

self = [super initWithNibName:nil bundle:nil];

如果你有一个与视图控制器类同名的 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:

 self = [super init];

means that you are just creating a naked view from scratch. not loading one from a nib.

try this instead:

self = [super initWithNibName:nil bundle:nil];

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 of viewDidLoad.

In your specific case, just put everything that is currently in viewDidLoad into loadView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文