2017-07-03 54 views
0

我想从一个视图控制器(包含集合视图)传递图像到另一个视图控制器,但它不会发生。我是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]; 
} 
在我的第二个视图控制器

...

- (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 
+0

为什么不' - (void)setImage:(UIImage *)image self.imageView.image = image; //这里显示为nil }'并尝试在您的viewController中声明'DetailedAlbumViewController * davc',在那里您正在展示 –

+0

因为同样的事情正在发生..然后我想我可以为它分配一些内存。所以我确实喜欢这样.. –

+0

对不起,再次询问...我初始化davc作为属性..它不工作.. –

回答

1

你只能通过图像未设置图像到目的地控制呃。所以你需要编码如下。

在DetailedAlbumViewController

@property (nonatomic, strong) UIImage *yourImage ; 

在viewDidLoad中DetailedAlbumViewControllerv

[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]; 
} 
+0

谢谢..其工作.... –

+0

欢迎.... @ VenuPendota – KKRocks

+0

然后标记为接受 – KKRocks

0

请检查你可能有self.imageView

0

只需在DetailedAlbumViewController中创建一个UIImage属性即可。

@property (nonatomic, strong) UIImage *someImage ; 

接下来将当前图像分配给它,如下面的代码片段所示。

- (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.someImage = image; 

[self presentModalViewController:davc animated:YES]; 
} 
+0

谢谢..其工作.... –

0

创建于DetailedAlbumViewController头文件属性,把它作为selectedImage

@property(nonatomic, strong) UIImage *selectedImage; 

更改您的实现像下面

- (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.selectedImage = image; 


    [self presentModalViewController:davc animated:YES]; 
} 

DetailedAlbumViewController实现文件viewWillAppear更新您的ImageView的图像。

并确保您的imageView不是nil。从Storyboard/xib否则连接UIImageView分配和它在viewWillAppear

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    if (self.imagView == nil) { 
     //allocate and position the imageView 
     self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    } 
    self.imageView.image = self.selectedImage; 
} 
+0

谢谢..其工作.... –

0

的UIImage *图像= cell.imageView的时间位置。图片;

图像可以是零,你需要确认一次,如果图片即将那么你可以把

if (self.imagView == nil) { 
      //allocate and position the imageView 
      self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     } 
self.imageView.image = self.selectedImage; 

内 viewDidAppear而不是viewWillAppear中的方法。

相关问题