ios – 多个UIAlertView问题

前端之家收集整理的这篇文章主要介绍了ios – 多个UIAlertView问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码有问题,我有两个UIAlertViews代码块,一个带取消和ok按钮,另一个带UI ImagePicker

-(IBAction)publicaPeticion
 {
    if([txtPeticion hasText] )
    {

        UIAlertView *alerta = [[UIAlertView alloc]
                              initWithTitle:@"Confirmación de Compra" 
                              message:@"Deseas comprar la petición por $12.00" 
                              delegate:self 
                              cancelButtonTitle:@"Cancelar"
                              otherButtonTitles:@"Aceptar",nil];
        [alerta show];  
    }


}

问题出在publicaPeticion和cargaImagen之间

-(IBAction)cargaImagen
{

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Subir una imagen" 
                          message:@"¿De donde deseas subir una imagen?" 
                          delegate:self 
                          cancelButtonTitle:@"Cancelar" 
                          otherButtonTitles:@"Desde el equipo",@"Tomar con camara",nil];
    [alert show];


}

以及从照片流或相机获取图像来源的方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
        picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];

    }
    if(buttonIndex ==2)
    {
        picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentModalViewController:picker animated:YES];
    }
}

问题是,当我按下“Aceptar”按钮(OK)时,它会将我从照片库中上传图片

也许是一个有点傻的问题,但我怎么能区分它?

解决方法

几种方式.

1)

看看如何调用委托方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

从alertView参数中,您可以确定调用哪个警报(如果您将cargaImagen和publicaPeticion的警报视图设置为单独的实例变量).

2)

您可以做的另一件事(也可能更容易)是在alertView上设置标签属性.

在您的’cargaImagen’方法中以及创建UIAlert之后,通过alert.tag = 1;将标记设置为1.

然后,在alertView:clickedButtonAtIndex:委托方法中,当alertView.tag == 1时,你会知道它来自cargaImagen,如果它是2(或零),你知道它来自publicaPeticion.

猜你在找的Xcode相关文章