IOs: Why IBOutlets are not connected after [[alloc] init], but after calling viewDidUnload:

I have a simple UINavigationViewControllerone that, when I select a specific element, creates a modal view in which it is located inside it UIImageView(PostcardViewController). However, if I call

PostcardViewController *postcardViewController = [[PostcardViewController alloc] init];
postcardViewController.imageView.image = image; 
[self.navigationController presentModalViewController:postcardViewController animated:YES];

postcardViewController.imageViewis nil and the image is never displayed. If I switch the last two lines and do this:

PostcardViewController *postcardViewController = [[PostcardViewController alloc] init];
[self.navigationController presentModalViewController:postcardViewController animated:YES];
postcardViewController.imageView.image = image; 

postcardViewController.imageViewinstalled and it displays well. Everything is connected in Interface Builder, and the PostcardViewController does not have any special code in it. After debugging, I came to the conclusion that after [ viewDidLoad] was called, that imageView was connected, and [ viewDidLoad] received the call when I called [ presentModalViewController].

, - ? , , , , .

+3
3

alloc + init, . , , . , :

PostcardViewController *postcardViewController = [[PostcardViewController alloc] init];
postcardViewController.view; // Forces the view to be loaded
postcardViewController.imageView.image = image; // Will no longer be nil
[self.navigationController presentModalViewController:postcardViewController animated:YES];
+7

, nib/xib. (, ) .

PostcardViewController imageView viewDidLoad. , UIImage.

PostcardViewController *pvc= [[PostcardViewController alloc] init];
pvc.image = image; 
[self.navigationController presentModalViewController:pvc animated:YES];
[pvc release];

PostcardViewController.h

  ...
  UIImage *image
}
@property (nonatomic, retain) UIImage *image;

PostcardViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    imageView.image = image;
}
+4

, UIViewControllers . , , .

+4

Source: https://habr.com/ru/post/1773861/


All Articles