1

I have two method to appear and disappear UIAlertView

- (void)showAlert {
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert"
                                                      message:@"Do you want to continue?"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                            otherButtonTitles:@"No", @"Yes", nil];
    [myAlert show];
}

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

The problem that I have, when I want to call my dismiss method, I don't know how to pass myAlert to dismiss method in order to hide the UIAlertView.

[self dismiss: // how to pas myAlert];
Evgeny Aleksandrov
  • 780
  • 14
  • 32
Steven
  • 762
  • 1
  • 10
  • 27
  • Do you want to dismiss alertview automatically without selection Yes or no?Please be clear and concise – Arun May 03 '17 at 10:49
  • 1
    UIAlertView is deprecated. Please, see [this answer](http://stackoverflow.com/questions/4463806/adding-a-simple-uialertview/28383410#28383410) for an up to date example. – FreeNickname May 03 '17 at 11:03

3 Answers3

2

UIAlertView is deprecated use UIAlertController:

use following syntax:

     UIAlertController* alert = [UIAlertController
                                                alertControllerWithTitle:@"SUCCESS"
                                                message:@"Profile picture updated successfuly."
//automatically 2 sec alert will disappear                                                preferredStyle:UIAlertControllerStyleAlert];
                    [self performSelector:@selector(abc:) withObject:alert afterDelay:2];

                    UIAlertAction* ok = [UIAlertAction
                                         actionWithTitle:@"OK"
                                         style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction * action)
                                         {
                                         }];

                    [alert addAction:ok];
                    [self presentViewController:alert animated:YES completion:nil];

when u want to dismiss ur UIAlertController:

-(void)abc:(UIAlertController*)x{
    [x dismissViewControllerAnimated:YES completion:nil];
}
NAVEEN KUMAR
  • 669
  • 6
  • 25
2

You need to create UIAlertView object globally.

YourController.h

@property (strong, nonatomic) UIAlertView *myAlertView;

YourController.m

-(void)showAlert
{
myAlertView = nil;
myAlertView = [[UIAlertView alloc] initWithTitle:@"My Alert"
                                                      message:@"Do you want to continue?"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                            otherButtonTitles:@"No", @"Yes", nil];
    [myAlert show];
}

-(void)dismiss
{
        [myAlertView dismissWithClickedButtonIndex:0 animated:YES];
}
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
0

@steven you don't need to create any method for dismiss alert. but i think you also check below link because UIAlertview is deprecated you can use UIAlertcontroller instead of it. Link: UIAlertView first deprecated IOS 9

Community
  • 1
  • 1