确保在调用 loadView 之前已完全设置 UIViewController

发布于 2024-12-11 05:46:08 字数 980 浏览 0 评论 0原文

有一个使用 UIImageViewUIViewController,并且该图像视图是使用图像数据 (NSData) 初始化的。它不使用 XIB,而是以编程方式创建其视图:

- (void)loadView
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:self.imageData]];
    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.contentSize = imageView.bounds.size;
    scrollView.delegate = self;
    [scrollView addSubview:scrollView];
}

该数据必须由另一个控制器来设置,该控制器会分配、初始化,并将该视图控制器推送到导航控制器:

ImageViewController *imageViewController = [ImageViewController alloc] init];
imageViewController.imageData = someData;
[self.navigationController pushViewController:imageViewController animated:YES];

我如何知道需要完成的所有事情(在本例中是设置数据)是在调用 loadView 之前完成的?或者,我不知道吗,我必须创建一个自定义初始化程序,或者在视图控制器收到数据时再次调用 loadView

我遇到过许多类似的情况,我对将要发生的事情感到困惑,例如 UITableViewController。

There is a UIViewController that uses a UIImageView, and that image view is initialized with image data (NSData). It does not use a XIB, but creates its view programmatically:

- (void)loadView
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:self.imageData]];
    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.contentSize = imageView.bounds.size;
    scrollView.delegate = self;
    [scrollView addSubview:scrollView];
}

That data has to be set by another controller which allocs, inits, and pushes this view controller onto the navigation controller:

ImageViewController *imageViewController = [ImageViewController alloc] init];
imageViewController.imageData = someData;
[self.navigationController pushViewController:imageViewController animated:YES];

How do I know that everything that needs to be done, which in this case, is setting the data, is done before loadView is called? Or, do I not know, and I have to create a custom initializer, or somehow call loadView again when the view controller receives the data?

I have faced many similar situations where I was confused about what will happen, such as with UITableViewControllers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

七色彩虹 2024-12-18 05:46:08

我如何知道需要完成的所有事情(在本例中是设置数据)都在调用 loadView 之前完成?

因为文档提到视图控制器在需要时才会加载视图。在导航控制器尝试将视图推送到屏幕上之前,不需要视图控制器的视图。

此外,将 imageData 分配给图像视图的正确位置可能是 viewDidLoad (“如果您想对视图执行任何其他初始化,请在 >viewDidLoad 方法。”)。并且您的 loadView 方法不会执行任何当前形式可见的操作。您必须在该方法中将视图分配给视图控制器的 view 属性。

How do I know that everything that needs to be done, which in this case, is setting the data, is done before loadView is called?

Because the documentation mentions that view controllers do not load their views until they are needed. And the view controller's view is not needed before the navigation controller tries to push it on screen.

Besides, the proper place for assigning the imageData to your image view is probably viewDidLoad ("If you want to perform any additional initialization of your views, do so in the viewDidLoad method."). And your loadView method will not do anything visible in its current form. You have to assign a view to the view controller's view property in that method.

以往的大感动 2024-12-18 05:46:08

当访问视图控制器的 view 属性时,会发生 loadView 。您编写的代码可以正常工作,因为第一次访问视图属性将位于pushViewController 内部的某个位置。

如果你这样写,就会遇到问题:

ImageViewController *imageViewController = [ImageViewController alloc] init];
NSLog(@"size = (%.0f, %.0f)", imageViewController.view.frame.size.width,
                              imageViewController.view.frame.size.height);
imageViewController.imageData = someData;
[self.navigationController pushViewController:imageViewController animated:YES];

因为你访问 NSLog 中的视图属性。这将导致 loadView 在设置 imageData 之前被调用。

loadView will happen when the view property of the view controller is accessed. The code you wrote will work fine, because the first time the view property will be accessed will be somewhere inside pushViewController.

If you wrote this you'd have a problem:

ImageViewController *imageViewController = [ImageViewController alloc] init];
NSLog(@"size = (%.0f, %.0f)", imageViewController.view.frame.size.width,
                              imageViewController.view.frame.size.height);
imageViewController.imageData = someData;
[self.navigationController pushViewController:imageViewController animated:YES];

because you access the view property in the NSLog. That would cause loadView to get called before imageData was set.

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