简体   繁体   English

iOS-将代码设置为4个UIImageViews

[英]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. 在我的应用中,有4个UIButton,它们都连接到4个UIAlerView,因此当按下按钮时,应该弹出一个UIAlertView。 This works fine with all UIButtons. 这适用于所有UIButton。 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. UIAlertView中的一个选项应打开照片库,以便用户可以更改连接到每个UIButton的4个UIImageView的图片。 the problem is that when I Choose a picture from any from the UIButtons the photo applies to just one ImageView, the fourth. 问题是,当我从UIButtons中选择图片时,该照片仅应用于一个ImageView,而第四个仅应用于。 Here is my code for the UIAlertView: 这是我的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. 它是完全相同的4倍,只是标记的值不同。 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 . 假设您还为图像视图提供了与按钮/警报相同的标签,则需要在图像选择器控制器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. 您可以将变量保留为int lastButtonTag以便知道最后按下哪个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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