简体   繁体   中英

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 ). 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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