2012-07-23 86 views
1

我有幾個按鈕。當任一按鈕被按下時,它彈出,alertView要求用戶拍攝照片或從相機膠捲中選擇。現在我遇到的問題是我有12個按鈕和12個UIImageViews。所有按鈕都有自己的操作,彈出警報並允許用戶選擇任一選項。現在,在didFinishPickingMediaWithInfo方法中,我將圖像從相機或相機膠捲傳遞到第一個imageView。這一切工作正常,但是,如果我想選擇按鈕2與另一個標籤觸發另一個警報我想設置imageView 2等等(不替換imageView1)。我需要一種方法來區分didFinishPickingMediaWithInfo基於彈出警報的按鈕選擇設置的imageView。因爲目前只有在選擇另一個應設置相應圖像的按鈕時,第一個圖像視圖纔會被設置和重置。UIImagePicker設置了多個圖像視圖

繼承人按鈕的操作。

-(IBAction) addPhoto1:(id) sender { 

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Image Source" message:@"Take a photo or select a previously taken photo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Take Photo", @"Select Photo", nil]; 
    [alert show]; 
    alert.tag = 101; 
    [alert release]; 

} 

,並提醒clickedButtonAtIndex

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    if (alert.tag == 101) { 

     if (buttonIndex == 1) { 
      //Take photo 
      [self performSelector:@selector(takePicture:) withObject:nil afterDelay:0.0]; 
     } 
     else if (buttonIndex == 2){ 
      //Camera roll 
      [self performSelector:@selector(pictureAlbum:) withObject:nil afterDelay:0.0]; 
     } 
     else if (buttonIndex == 0) { 
      NSLog(@"Cancel"); 
     } 
    } 
} 

而這裏的didFinishPickingMediaWithInfo

-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 

     UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
     NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 

     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 


     [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:dict completionBlock:nil]; 


     if (addFirstImage.tag == 1001) { 
      firstImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
      firstImage.layer.cornerRadius = 5; 
      firstImage.layer.masksToBounds = YES; 

     } 
     if (addSecondImage.tag == 1002) {    
      secondImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
      secondImage.layer.cornerRadius = 5; 
      secondImage.layer.masksToBounds = YES; 

     } 
    } 
} 

現在很明顯,這是不對的最新設置正確的imageViews形象的最佳方式基礎上的按鈕來自最初被按下的警報? (addFirstImage和addSecondImage都通過按鈕鏈接IB)

非常感謝

回答

0

你爲什麼不使用動作的'發送」到‘記憶’的按鈕最初按壓? 使用IBOutletCollections,同指標下令:

@property (...) IBOutletCollection(UIButton) allYourButtons; 

@property (...) IBOutletCollection(UIImageView) allYourImageViews; 
// I suppose that both Collections objects have 'tags' (set in XIB file) 
// a UIButton and the corresponding UIImageVIew have the SAME tag 

@property (...) NSInteger clickedButtonIndex; 

- (IBAction) imageViewWasPressed:(id)sender 
{ 
    //keep a reference to the specific button that launched the action 
    self.clickedButtonIndex = [(UIButton *)sender tag]; 

    [self presentAlertViewForPictureChoice]; 
} 

然後,使用​​在UIAlertView委託方法找到它的UIImageView從allYourImageViews應該有它的圖像更新中...

- (UIImageView *)imageToUpdateForSelectedIndex:(NSInteger)anIndex 
{ 
    UIImageView *result = nil; 
    for (UIImageView *imageView in self.allYourImageViews){ 
     if(imageView.tag == anIndex){ 
      result = imageView; 
      break; 
     } 
    } 
    return result; 

} 

(EDIT ) 好的,另一種將UIButton與UIImageView關聯的方法是創建一個子類

// a new UIButton subclass 

ButtonWithImageView : UIButton 

@property (...) IBOutlet UIImageView *relatedImageView; 

//IBOutlet enables you to directly link a Button with its imageVIew in your XIB 

// in your controller 

@property (...) ButtonWithImageView *selectedButton; 


- (IBAction)buttonWasPressed:(id)sender 
{ 
    ... 
    self.selectedButton = (ButtonWithImageView *)sender; 

} 

然後,所有你需要做的就是編輯self.selectedButton.relatedImageView

+0

Humm不知道..我只是需要一種方法來知道按鈕被按下,以便它在didfinishpickingmediawithinfo中設置正確的圖像視圖。像if(action1被觸發),然後設置圖像視圖一如果(action2被觸發)然後設置圖像視圖2等等。 – 2012-07-23 12:45:05

0

好吧,我想通了,如何做到這一點。首先我創建稱爲myTag接口文件

int myTag; 

我然後標記我與標籤號碼的所有按鈕的實例變量INT即button1.tag = 1001(以1012爲我的12個按鈕)在viewDidLoad中,

我然後創建了一個Action,而不是每個按鈕的12個。並通過IB連接所有這一切。

- (IBAction)takePhoto:(UIButton*)sender { 

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Image Source" message:@"Take a photo or select a previously taken photo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Take Photo", @"Select Photo", nil]; 
[alert show]; 
alert.tag = 101; 
[alert release]; 

if (sender.tag == 1001){ 

    NSLog(@"You clicked button 1"); 
    myTag = 1001; 

} 

if (sender.tag == 1002){ 

    NSLog(@"You clicked button 2"); 
    myTag = 1002; 
} 

}

然後我發現按下哪個按鈕和一個int值傳遞給敏伊娃

,然後在我的didFinishPickingMediaWithInfo我這樣做!

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 


    [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:dict completionBlock:nil]; 

    if (myTag == 1001){ 


     firstImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     firstImage.layer.cornerRadius = 5; 
     firstImage.layer.masksToBounds = YES; 

    } 

    if (myTag == 1002) { 
     secondImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     secondImage.layer.cornerRadius = 5; 
     secondImage.layer.masksToBounds = YES; 
    } 





    [picker dismissModalViewControllerAnimated:YES]; 

} 

所有工作都很完美!非常感謝您的幫助!

相關問題