dismissing a UIAlertView programmatically

后端 未结 5 1556
孤城傲影
孤城傲影 2021-02-13 17:12

I need help on dismissing a UIAlertView programmatically. Currently I have this

UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@\"title\" message:@

相关标签:
5条回答
  • 2021-02-13 17:14

    I encountered this problem too. In my case, for some reason calling:

    [alert dismissWithClickedButtonIndex:0 animated:NO];
    

    didn't work always (yes, even calling it on UI thread and yes, alert != nil), instead simply setting the animated flag to YES it worked:

    [alert dismissWithClickedButtonIndex:0 animated:YES];
    

    Maybe it's an Apple bug...

    0 讨论(0)
  • 2021-02-13 17:18

    you should display it first:

    UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert1 show];
    

    then in delegate method

    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
        if(buttonIndex==0){
         // do something
        }
    }
    
    0 讨论(0)
  • 2021-02-13 17:18

    You can use the delegate method -alertView:didDismissWithButtonIndex: instead—it gets called once the alert view’s been removed from the screen, OR better approach is , use a background thread, e.g. with -performSelectorInBackground:withObject:, to handle whatever processing you need to do.

    0 讨论(0)
  • 2021-02-13 17:23

    You need to set two things.

    1. include your .h file : <UIAlertViewDelegate>

    2. please follow below implementation...

       UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; 
            [alert1 show];
            [self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.0];
    

    the dismiss method will be...

    -(void)dismiss:(UIAlertView*)alert
    {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
    

    I hope this will help you.

    0 讨论(0)
  • 2021-02-13 17:39

    The methods you called is correct.
    I guess the alert1 is nil when your call the method dismissWithClickedButtonIndex:animated:
    Try to check your variable alert1.

    0 讨论(0)
提交回复
热议问题