2015-02-07 60 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

相關問題