确保在调用 loadView 之前已完全设置 UIViewController
有一个使用 UIImageView
的 UIViewController
,并且该图像视图是使用图像数据 (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 alloc
s, init
s, 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 UITableViewController
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为文档提到视图控制器在需要时才会加载视图。在导航控制器尝试将视图推送到屏幕上之前,不需要视图控制器的视图。
此外,将
imageData
分配给图像视图的正确位置可能是viewDidLoad
(“如果您想对视图执行任何其他初始化,请在>viewDidLoad
方法。”)。并且您的loadView
方法不会执行任何当前形式可见的操作。您必须在该方法中将视图分配给视图控制器的view
属性。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 probablyviewDidLoad
("If you want to perform any additional initialization of your views, do so in theviewDidLoad
method."). And yourloadView
method will not do anything visible in its current form. You have to assign a view to the view controller'sview
property in that method.当访问视图控制器的 view 属性时,会发生 loadView 。您编写的代码可以正常工作,因为第一次访问视图属性将位于pushViewController 内部的某个位置。
如果你这样写,就会遇到问题:
因为你访问 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:
because you access the view property in the NSLog. That would cause loadView to get called before imageData was set.