1

我有几个按钮。当按下任一按钮时,它会弹出并 alertView 要求用户拍照或从相机胶卷中选择。现在我遇到的问题是我有 12 个按钮和 12 个 UIImageViews。所有按钮都有自己的动作,可以弹出警报并允许用户选择任一选项。现在在 didFinishPickingMediaWithInfo 方法中,我将图像从相机或相机胶卷传递到第一个 imageView。这一切都很好,但是如果我想选择按钮 2,它会触发另一个带有另一个标签的警报,我想设置 imageView 2 等等(不替换 imageView1)。我需要一种方法在 didFinishPickingMediaWithInfo 中根据弹出警报的按钮选择来区分要设置的 imageView。

这是按钮的操作。

-(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 链接的按钮)

非常感谢

4

2 回答 2

0

好的,我想出了如何做到这一点。首先,我在接口文件中创建了一个名为 myTag 的 ivar int

int myTag;

然后我在 viewDidLoad 中用标签号标记了我的所有按钮,即 button1.tag = 1001(我的 12 个按钮为 1012),

然后,我为每个按钮创建了一个 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 值传递给 myInt ivar

然后在我的 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];

}

一切正常!非常感谢您的帮助!

于 2012-07-23T22:23:05.633 回答
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];
}

然后,您clickedButtonIndexUIAlertView委托方法中使用来查找allYourImageViews应该更新其图像的 UIImageVIew ...

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

}

(编辑) 好的,将 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

于 2012-07-23T12:13:11.713 回答