问题描述
我有一个要显示在 UIView
中的图像.在Interface Builder中, UIView
是根,而 UIImageView
是其子代.
I have an image I want to display inside a UIView
. In Interface Builder, the UIView
is the root and a UIImageView
is its child.
视图连接到视图控制器的视图出口,图像视图连接到图像视图出口:
The view is connected to view controller's view outlet, and the image view is connected to the image view outlet:
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
如果我尝试在 UIImageView
的图像属性设置为可见之前,就不会显示该图像.
If I try to set the image property of UIImageView
before it's visible, the image doesn't show up.
TestView *testView = [[TestView alloc] initWithNibName:@"TestView" bundle:nil];
testview.imageView.image = [logos objectAtIndex:indexPath.row];
[self.navigationController pushViewController:testView animated:YES];
但是,如果我将图像传递给控制器并在 viewDidLoad
中设置image属性,则图像变为可见.
If, however, I pass the image to the controller and set the image property in viewDidLoad
, the image becomes visible.
TestView *testView = [[TestView alloc] initWithNibName:@"TestView" bundle:nil];
testview.image = [logos objectAtIndex:indexPath.row];
[self.navigationController pushViewController:testView animated:YES];
- (void)viewDidLoad
{
[super viewDidLoad];
imageView.image = image;
}
是什么导致图片在第一种情况下无法显示?
What is causing the image to not show up in the first scenario?
推荐答案
UIViewController
的 initWithNibName:bundle:方法指出:
nib/xib文件没有立即加载的结果(即,它使用了所谓的 lazy loading ),这意味着TestView控制器中的imageView IBOutlet
属性仍然是在第一个示例中尝试将其设置为nil.
A consequence of the nib/xib file not being loaded immediately (i.e. it uses so called lazy loading) means that the imageView IBOutlet
property in your TestView controller is still nil when you try to set it in your first example.
在第二个示例中,将其设置在 viewDidLoad
中,该文件在加载了nib/xib文件后称为 ,因此可以按预期工作.
In your second example you are setting it in the viewDidLoad
which is called after the nib/xib file is loaded, and therefore it works as you expect.
这篇关于属性设置得太早时,UIImageView不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!