2012-07-23 90 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]; 

} 

所有工作都很完美!非常感谢您的帮助!