-2

我试图用 4 个 ImageViews 和 4 个 UIButtons 做一个应用程序。当按下其中一个 UIButtons 时,应显示 UIAlertView 并带有 3 个选项。一个名为“图片”的应该打开照片库,以便用户可以更改 ImageView 图片。我为此编写了一个代码,但它不会工作。有人对如何使它工作有任何建议吗?提前致谢!

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *buttonOne = changeButtonOne;  //the UIButtons
NSString *buttonTwo = changeButtonTwo;
NSString *buttonThree = changeButtonThree;
NSString *buttonFour = changeButtonFour;


if([buttonOne isEqualToString:@"buttonOne"])
{
      if([title isEqualToString:@"Done"])
    {
        NSLog(@"You pressed done");
    }
      else if([title isEqualToString:@"Phone number"])
    {
        NSLog(@"You pressed Phone number");
    }
      else if([title isEqualToString:@"Picture"])
    {
        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];
    }
}
else if([buttonTwo isEqualToString:@"buttonTwo"])
{
    if([title isEqualToString:@"Done"])
    {
        NSLog(@"Yoy pressed done");
    }
    else if([title isEqualToString:@"Phone number"])
    {
        NSLog(@"You pressed Phone number");
    }
    else if([title isEqualToString:@"Picture"])
    {
        imagePickerController2 = [[UIImagePickerController alloc]init];  //I think its the part thats wrong.
        [imagePickerController2 setDelegate:self];
        [imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:imagePickerController2 animated:YES completion:nil];
    }

}
else if([buttonThree isEqualToString:@"buttonThree"])
    {
        if([title isEqualToString:@"Done"])
        {
            NSLog(@"You pressed done");
        }
        else if([title isEqualToString:@"Phone number"])
        {
            NSLog(@"You pressed Phone number");
        }
        else if([title isEqualToString:@"Picture"])
        {
            imagePickerController3 = [[UIImagePickerController alloc]init];   //i think this is the wrong part
            [imagePickerController3 setDelegate:self];
            [imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [self presentViewController:imagePickerController3 animated:YES completion:nil];
        }
    }

    else if([buttonFour isEqualToString:@"buttonFour"])
    {
        if([title isEqualToString:@"Done"])
        {
            NSLog(@"You pressed done");
        }
        else if([title isEqualToString:@"Phone number"])
        {
            NSLog(@"You pressed Phone number");
        }
        else if([title isEqualToString:@"Picture"])
        {
            imagePickerController4 = [[UIImagePickerController alloc]init];    //this is probably the wrong part
            [imagePickerController4 setDelegate:self];
            [imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [self presentViewController:imagePickerController4 animated:YES completion:nil];
        }
    }




}

@end
4

1 回答 1

0

您应该使用tagUIAlertView 的属性来区分警报和buttonIndex(不是按钮标题)以了解按下了哪个按钮。所以不要在警报视图委托方法中使用 isEqualToString 。您希望它适用于不同的语言。

在按钮操作中,在显示警报之前:

alertView.tag = 1; // 1 = button1, 2 = button2 etc.

警报视图委托:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) { //button1
        if (buttonIndex == alertView.cancelButtonIndex) {
            // handle cancel button
        }        
        if (buttonIndex == alertView.firstOtherButtonIndex) {
            // handle action 1
        }
        if (buttonIndex == alertView.firstOtherButtonIndex+1) {
            // handle action 2
        }
    }
    if (alertView.tag == 2) { //button2
        if (buttonIndex == alertView.cancelButtonIndex) {
            // handle cancel button
        }        
        if (buttonIndex == alertView.firstOtherButtonIndex) {
            // handle action 3
        }
        if (buttonIndex == alertView.firstOtherButtonIndex+1) {
            // handle action 4
        }
    }
}
于 2012-11-20T21:26:55.837 回答