Steven
Steven

Reputation: 782

Hide UIAlertView In objective -c

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];

Upvotes: 1

Views: 1857

Answers (3)

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

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];
}

Upvotes: 2

NAVEEN KUMAR
NAVEEN KUMAR

Reputation: 667

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];
}

Upvotes: 2

@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

Upvotes: 0

Related Questions