属性设置得太早时,UIImageView不显示图像 [英] UIImageView not displaying image when property is set too early

查看:63
本文介绍了属性设置得太早时,UIImageView不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要显示在 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

...您指定的nib文件不会立即加载.首次访问视图控制器的视图时将加载它.如果要在加载nib文件后执行其他初始化,请覆盖 viewDidLoad 方法并在其中执行任务.…

… The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there. …

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不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆