简体   繁体   English

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

[英]Ensuring that a UIViewController is fully set up before loadView is called

There is a UIViewController that uses a UIImageView , and that image view is initialized with image data ( NSData ). 有一个使用UIImageViewUIViewController ,该图像视图使用图像数据( NSData )初始化。 It does not use a XIB, but creates its view programmatically: 它不使用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];
}

That data has to be set by another controller which alloc s, init s, and pushes this view controller onto the navigation controller: 该数据必须通过另一控制器,该控制器被设置alloc S, init S,并推动该视图控制器到导航控制器:

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

I have faced many similar situations where I was confused about what will happen, such as with UITableViewController s. 我遇到过许多类似的情况,我对将要发生的事情感到困惑,例如使用UITableViewController

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? 我如何知道需要完成的所有事情(在本例中是设置数据)是在调用loadView之前完成的?

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."). 此外,将imageData分配给图像视图的正确位置可能是viewDidLoad (“如果要对视图执行任何其他初始化,请在viewDidLoad方法中执行此操作。”)。 And your loadView method will not do anything visible in its current form. 并且你的loadView方法不会以当前形式做任何可见的事情。 You have to assign a view to the view controller's view property in that method. 您必须在该方法中为视图控制器的view属性分配视图。

loadView will happen when the view property of the view controller is accessed. 访问视图控制器的视图属性时将发生loadView。 The code you wrote will work fine, because the first time the view property will be accessed will be somewhere inside pushViewController. 您编写的代码将正常工作,因为第一次访问view属性将在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. 因为您访问NSLog中的视图属性。 That would cause loadView to get called before imageData was set. 这将导致在设置imageData之前调用loadView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM