2015-02-07 62 views
1

在我的应用程序中,我一次选择五个图像并在小视图中显示,如果单击该按钮,则该按钮中的5个图像应该转到下一个视图控制器。但是当我单击该按钮时只有第0个索引中的图像通过,剩余图像没有通过。希望有人帮助我如何将多个图像一次传递给其他视图?

这里是我的code.This代码是用于从第一个视图传递到第二个视图和chosenImages是一个数组,其中所有选取的图像要保存

SecondViewController *secview=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
if (secview.imgView.tag==0) { 
secview.img=[self.chosenImages objectAtIndex:0]; 
} 
else if (secview.imgView1.tag==1) 
{ 
secview.img=[self.chosenImages objectAtIndex:1]; 

} 
else if (secview.imgView1.tag==2) 
{ 
secview.img=[self.chosenImages objectAtIndex:2]; 

} 
else if (secview.imgView1.tag==3) 
{ 
secview.img=[self.chosenImages objectAtIndex:3]; 

} 
else if (secview.imgView1.tag==4) 
{ 
secview.img=[self.chosenImages objectAtIndex:4]; 

} 
NSLog(@"%@",secview.img); 
[self presentViewController:secview animated:YES completion:nil]; 

和第二视图我的代码是:

if (imgView.tag==0) 
    { 
     imgView.image=img; 
    } 
    else if (imgView1.tag==1) 
    { 
     imgView1.image=img; 
    } 
    else if (imgView2.tag==2) 
    { 
     imgView2.image=img; 
    } 
    else if (imgView3.tag==3) 
    { 
     imgView3.image=img; 
    } 
    else if (imgView4.tag==4) 
    { 
     imgView4.image=img; 
    } 

更新:在didFinishPickingMedia我写了这个代码

images = [NSMutableArray arrayWithCapacity:[info count]]; 
for (NSDictionary *dict in info) { 
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){ 
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){ 
image=[dict objectForKey:UIImagePickerControllerOriginalImage]; 
[images addObject:image]; 
UIImageView *imageview = [[UIImageView alloc] initWithImage:image]; 
[imageview setContentMode:UIViewContentModeScaleAspectFit]; 
} 

} 
else { 
NSLog(@"UIImagePickerControllerReferenceURL = %@", dict); 
} 
} 
self.chosenImages = images; 
[AllImages setHidden:NO]; 
previousButtonTag=1000; 
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 4,  self.AllImages.frame.size.width, 104)]; 
int x =0.5; 
for (int i=0; i<5; i++) { 
     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(x, 0, 100, 123); 
     button.layer.borderWidth=0.5; 
     button.titleLabel.font=[UIFont boldSystemFontOfSize:12]; 
     button.titleLabel.textAlignment = NSTextAlignmentCenter; 
     button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     // button.layer.borderColor=[[UIColor lightGrayColor]CGColor]; 
     button.tag = i; 
     [button setBackgroundImage:[self.chosenImages objectAtIndex:i] forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(OverLayMethod:) forControlEvents:UIControlEventTouchUpInside]; 
     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     [scrollView addSubview:button]; 
     x += button.frame.size.width; 
     x=x+6.0; 
    } 
    scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height); 
    scrollView.backgroundColor = [UIColor clearColor]; 
    [self.AllImages addSubview:scrollView]; 
} 
+0

创建您destinationview多一个UIImage对象,并通过图像与该对象。 – 2015-02-07 11:57:45

+0

谢谢你的回复@Darshan Kunjadiya ..img是第二视图本身的对象 – 2015-02-07 12:01:02

回答

1

我通过在第二个视图中声明另一个数组解决了我的问题。在第一个视图中,我们将所选图像保存在一个数组中.so第一个视图数组对象=第二个视图数组,我在第二个视图中将该数组传递给我的代码 第二种观点:

for (int i=0; i<[arr count]; i++) { 
self.img=[arr objectAtIndex:i];  
if (i==0) 
{ 
imgView.image=img; 
} 
else if (i==1) 
{ 
imgView1.image=img; 
} 
else if (i==2) 
{ 
imgView2.image=img; 
} 
else if (i==3) 
{ 
imgView3.image=img; 
} 
else if (i==4) 
{ 
imgView4.image=img; 
} 
}  


} 

本我得到正确的输出

0

我不知道,我得到了你的问题,但我的理解是,你想送5个图像另一种观点。如果是这样,定义一个新的NSArray属性,如您SecondViewController“图像”,并把它添加到您从第一视图到第二

SecondViewController *secondVC = [SecondViewController new]; 
[secondVC setImages: self.chosenImages]; 

与您的代码的问题在哪里,您使用的if/else若块语句,如果if/else中的其中一个条件评估为true,则只有您进入块的其中一个条件。另外,-objectAtIndex:只返回一个项目。

[self.chosenImages objectAtIndex:0]; 

而且,我敢肯定,secview.img意味着你有你的SecondViewController内一个UIImage属性。

+0

谢谢你的回复@ user4130613如果我在此添加它显示nil对象传递 – 2015-02-09 05:38:37

相关问题