ios - 在 View Controller 之间传递图像在呈现模态视图时不起作用

标签 ios object

我试图将图像从一个 View Controller (包含 Collection View )传递到另一个 View Controller ,但它没有发生。我是 iOS objective-c 的新手。谁能告诉我解决方案。 这是我的代码...

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{


    DetailedAlbumViewController *davc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailedAlbumViewController"];

    AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    UIImage *image = cell.imageView.image;

    [davc setImage:image];

    //davc.imageView.image = image;


    [self presentModalViewController:davc animated:YES];
}

在我的第二个 View Controller 中...

- (void) setImage:(UIImage *) image{

    UIImage *someImage = [[UIImage alloc]init];
    someImage = image;       // image is passing upto here.. i.e. someImage is being allocated with the image passed from first view controller..

    self.imageView.image = someImage;     // here it is showing nil

}

我使用了调试控制台..输出是..

(lldb) po someImage
<UIImage: 0x79eb8590>, {500, 347}

(lldb) po self.imageView.image
 nil

最佳答案

您只能将图像而不是设置图像传递给目标 Controller 。所以你需要编写如下代码。

在 DetailedAlbumViewController 中

@property (nonatomic, strong) UIImage *yourImage ;

在DetailedAlbumViewControllerv的viewDidLoad中

[self setImage:self.yourImage];

点击单元格

建议 - 您需要使用 didselectItemAtIndexpath 而不是 didDeselectItemAtIndexPath。

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{


    DetailedAlbumViewController *davc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailedAlbumViewController"];

    AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    UIImage *image = cell.imageView.image;
    davc.yourImage = image

    [self presentModalViewController:davc animated:YES];
}

关于ios - 在 View Controller 之间传递图像在呈现模态视图时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44878223/

相关文章:

ios - 具有两个标识符的表格 View 单元格。对于 TableView 中的奇数一个单元格和甚至另一个单元格

ios - 如何使 Swift Class Singleton 实例线程安全?

iphone - 防止 UIDatePicker 与 TableView 一起滚动

iphone - 自定义警报的 UILocalNotification 重复间隔(星期日、星期一、星期二、星期三、星期四、星期五、星期六)

javascript - 下划线 map 对象

Javascript 用缺少的对象和值填充数组

iOS 从 nib 加载 View

Java对象序列化嵌套对象

c++ - 菜鸟,帮忙做个对象

解析 AMF3 对象