gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:17:41 24 4
gpt4 key购买 nike

我试图将图像从一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com