0

在我的应用程序中有 4 个 UIButtons,它们都连接到 4 个 UIAlerView,因此当按下按钮时,应该会弹出 UIAlertView。这适用于所有 UIButton。UIAlertView 中的选项之一应该打开照片库,以便用户可以更改连接到每个 UIButton 的 4 个 UIImageView 的图片。问题是,当我从任何 UIButtons 中选择图片时,该照片仅适用于一个 ImageView,即第四个。这是我的 UIAlertView 代码:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

//UIAlertView´s in the UIButtons setup
if (alertView.tag == 1) { //button1
    if (buttonIndex == alertView.cancelButtonIndex) {
        NSLog(@"Done");
    }
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        NSLog(@"Number");
    }
    if (buttonIndex == alertView.firstOtherButtonIndex+1) {

        imagePickerController = [[UIImagePickerController alloc]init];   //I think its this part which are wrong
        [imagePickerController setDelegate:self];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:imagePickerController animated:YES completion:nil];

    }
}
if (alertView.tag == 2) { //button2
    if (buttonIndex == alertView.cancelButtonIndex) {
        NSLog(@"Done");
    }
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        NSLog(@"Number");
    }
    if (buttonIndex == alertView.firstOtherButtonIndex+1) {

        imagePickerController = [[UIImagePickerController alloc]init];   //I think its this part which are wrong
        [imagePickerController setDelegate:self];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:imagePickerController animated:YES completion:nil];

    }
}
 if (alertView.tag == 3)
 {
     if (buttonIndex == alertView.cancelButtonIndex) {
         NSLog(@"Done");
     }
     if (buttonIndex == alertView.firstOtherButtonIndex){
         NSLog(@"Number");
     }
     if (buttonIndex == alertView.firstOtherButtonIndex+1){
         imagePickerController = [[UIImagePickerController alloc]init];   //I think its this part which are wrong
         [imagePickerController setDelegate:self];
         [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
         [self presentViewController:imagePickerController animated:YES completion:nil];
     }

 }
if (alertView.tag == 4);
{
    if (buttonIndex == alertView.cancelButtonIndex){
        NSLog(@"Done");
    }
    if (buttonIndex == alertView.firstOtherButtonIndex){
        NSLog(@"phone number");
    }
    if (buttonIndex == alertView.firstOtherButtonIndex+1){
        imagePickerController = [[UIImagePickerController alloc]init];
        [imagePickerController setDelegate:self];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}

}




@end
4

1 回答 1

1

首先,您的代码非常多余。除了标签的值之外,它是完全相同的 4 倍。使用一个变量并且只写一次代码!

其次,您引用的代码与问题无关。假设您还为图像视图提供了与按钮/警报相同的标签,您需要在图像选择器控制器回调中将图像分配给正确的图像视图。这是合乎逻辑的,因为您必须在分配图像之前知道选择了哪个图像。利用

imagePickerController:didFinishPickingMediaWithInfo:

您可以保留一个变量int lastButtonTag,以便知道最后按下哪个按钮。

于 2012-11-24T11:06:24.437 回答