繁体   English   中英

iOS-将代码设置为4个UIImageViews

[英]iOS - Set the code to 4 UIImageViews

在我的应用中,有4个UIButton,它们都连接到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倍,只是标记的值不同。 使用变量 ,只编写一次代码!

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

imagePickerController:didFinishPickingMediaWithInfo:

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

暂无
暂无

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

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