简体   繁体   中英

iOS - Set the code to 4 UIImageViews

In my app there are 4 UIButtons, which all are connected to 4 UIAlerView so when a button are pressed an UIAlertView should pop up. This works fine with all UIButtons. One of the options in the UIAlertView should open photo library so the user can change the picture of 4 UIImageView´s, which are connected to each UIButton. the problem is that when I Choose a picture from any from the UIButtons the photo applies to just one ImageView, the fourth. Here is my code for the 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

First, your code is extremely redundant. It is 4 times exactly the same, save for the value of the tag. Use a variable and write the code only once!

Second, the code you quote has nothing to do with the problem. Assuming that you also gave the image views the same tags as the buttons / alerts, you need to assign the image to the correct image view in the image picker controller callback . This is logical because you have to know which image is picked before assigning the image. Use

imagePickerController:didFinishPickingMediaWithInfo:

You could keep a variable int lastButtonTag so you know which button was last pressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM