【发布时间】:2015-07-12 17:44:27
【问题描述】:
我有一个 CollectionView,在 didSelectItemAtIndexPath 方法中我实例化了一个新的 ViewController,设置 ViewController 上的 String 属性来保存图像的名称。然后在新 ViewController 中的 viewDidLoad 或 viewDidAppear 方法中,我尝试使用 vc.imageName 属性设置图像。
代码如下:
// collection.m
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
ViewController *vc = [[ViewController alloc] init];
NSString *imageName = [colouringPhotos objectAtIndex:indexPath.row];
vc.imageName = imageName;
[self presentViewController:vc animated:YES completion:nil];
}
// ViewController.h
@property (nonatomic, strong) NSString * imageName;
//ViewController.m
@property (strong, nonatomic) IBOutlet UIImageView *lineImageView;
-(void) viewDidAppear:(BOOL)animated{
self.lineImageView.image = [UIImage imageNamed:self.imageName];
}
但图片不显示。我已经在 ImageView 上设置了背景颜色,以检查它是否显示在新的 ViewController 中,我可以看到它,但图像永远不会显示在 UIImageView 中。
【问题讨论】:
-
是的,当我在 viewDidLoad 中设置断点时,名称为空。所以名称没有被传递给新的 ViewController
-
如果我在 vc.imageName = imageName 处设置断点,我可以看到 imageName 在那里,但是一旦它被传递给 presentViewcontroller,它在 viewDidLoad 方法中为空
标签: ios uiviewcontroller uicollectionview