在prepareForSegue中设置UIImageView.image [英] set UIImageView.image in prepareForSegue
问题描述
我想选择一个具有UIImageView的photoViewController. 在flickrTVC中,我说:
I want to segue to a photoViewController that has an UIImageView. In flickrTVC, I say:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showPhoto"])
{
photoViewController *vc = [segue destinationViewController];
UIImage *photo = [UIImage imageWithContentsOfFile:@"images/en.jpeg"];
[vc setDisplayedPhoto:photo];
}
}
和setDisplayedPhoto方法的实现是:
and the setDisplayedPhoto method's implementation is:
-(void)setDisplayedPhoto:(UIImage *)image
{
[_imageView setImage:image]; //@property (nonatomic, strong) IBOutlet UIImageView *imageView;
}
但是当我进行搜索时,UIImageView是白色的...为什么会这样?
But when I do segue, the UIImageView is white... Why could this be?
推荐答案
您的目标视图控制器在源视图控制器收到prepareForSegue:sender:
消息时尚未加载其视图层次结构.因此,目标VC的_imageView
变量是nil
.
Your destination view controller hasn't loaded its view hierarchy by the time your source view controller receives the prepareForSegue:sender:
message. So your destination VC's _imageView
variable is nil
.
推荐的方法是将图像(或者可能更好的图像文件名)存储为目标VC的属性,然后在目标VC的viewDidLoad
或viewWillAppear:
方法中设置图像视图的图像
The recommended approach is to store the image (or, probably even better, the image file name) as a property of the destination VC, and then set the image view's image in the destination VC's viewDidLoad
or viewWillAppear:
method.
这篇关于在prepareForSegue中设置UIImageView.image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!