I have a simple UINavigationViewController which when a certain item is selected creates a modal view that has an UIImageView (PostcardViewController below) inside it.但是,如果我打电话

PostcardViewController *postcardViewController = [[PostcardViewController alloc] init];
postcardViewController.imageView.image = image;
[self.navigationController presentModalViewController:postcardViewController animated:YES];
postcardViewController.imageView 为零,图像永远不会显示。如果我切换后两行,并使其:
PostcardViewController *postcardViewController = [[PostcardViewController alloc] init];
[self.navigationController presentModalViewController:postcardViewController animated:YES];
postcardViewController.imageView.image = image;
postcardViewController.imageView 已设置,并且显示正常。一切都在 Interface Builder 中连接起来,PostcardViewController 中没有任何特定的代码。通过调试,我发现,在调用 [ viewDidLoad ] 之后,imageView 被连接起来,并且在我调用 [ viewDidLoad ] 时调用了 [ presentModalViewController ]。

这是为什么,而且,我在这里做错了什么?我想我应该在实际显示之前设置整个 View ,但截至目前,我必须先显示它才能完全设置它。

最佳答案

您只使用 alloc+init 创建了 View Controller ,而不是 View 本身。 View 是延迟加载的,即第一次使用。要强制在实际显示之前创建 View ,请执行以下操作:

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];

关于iphone - iOs:为什么在 [[alloc] init] 之后没有连接 IBOutlets,而是在 viewDidUnload: 被调用之后,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4134897/

10-14 22:31