我有一个警报视图,当我单击“是”按钮时,它应该会产生另一个警报视图和一个祝酒消息,但是这没有发生。我不知道。这是我的代码:

-(void)myMethod {
    UIAlertView *saveAlert = [[UIAlertView alloc] initWithTitle:@"First Message"
                                                        message:@"My First message"
                                                       delegate:nil
                                              cancelButtonTitle:@"No"
                                              otherButtonTitles:@"Yes", nil];
    saveAlert.tag=0;
    [saveAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}

这是我用来为不同警报视图提供功能的方法。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==0) {

        if (buttonIndex == 0)
        {
            //Code for Cancel button
        }
        if (buttonIndex == 1)
        {
            //code for yes button
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"Successfully displayed First Message";
            hud.margin = 10.f;
            hud.yOffset = 150.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:3];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Second Message"
                                                            message:@"My second message"
                                                           delegate:nil
                                                  cancelButtonTitle:@"No"
                                                  otherButtonTitles:@"Yes",nil];
            alert.tag=1;
            [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
        }
    }

    if (alertView.tag==1) {

        if (buttonIndex == 0)
        {
            //Code for Cancel button
        }
        if (buttonIndex == 1)
        {
            //Code for yes Button
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"Succesfully displayed Second Message";
            hud.margin = 10.f;
            hud.yOffset = 150.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:3];
        }
    }
}

任何人都可以帮助找到问题。在第一个警报中单击“是”按钮后,为什么我无法获得第二个警报?

最佳答案

您尚未为UIAlertView设置委托,也请确保您的委托符合UIAlertViewDelegate协议。在下面找到代码片段。

您的控制器符合UIAlertViewDelegate协议:

@interface YourViewController : UIViewController <UIAlertViewDelegate>

创建UIAlertView并设置deleagte:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"First Message"
                                                    message:@"Show second message"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
[alertView show];

实现UIAlertViewDelegate委托方法:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if( 0 == buttonIndex ){ //cancel button
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
   } else if ( 1 == buttonIndex ){
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
        UIAlertView * secondAlertView = [[UIAlertView alloc] initWithTitle:@"Second Message"
                                                                   message:@"Displaying second message"
                                                                  delegate:nil
                                                         cancelButtonTitle:@"OK"
                                                         otherButtonTitles:nil];
        [secondAlertView show];
    }
}

关于ios - UIAlertView按钮操作不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25298196/

10-10 20:36
查看更多