在 objective -c 中隐藏 UIAlertView

Hide UIAlertView In objective -c

我有两种显示和消失 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];
}

我遇到的问题是,当我想调用 dismiss 方法时,我不知道如何将 myAlert 传递给 dismiss 方法以隐藏 UIAlertView。

[self dismiss: // how to pas myAlert];

不推荐使用 UIAlertView 使用 UIAlertController:

使用以下语法:

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

当你想关闭你的 UIAlertController:

-(void)abc:(UIAlertController*)x{
    [x dismissViewControllerAnimated:YES completion:nil];
}

@steven 您不需要创建任何解除警报的方法。但我认为您还检查下面 link 因为 UIAlertview 已被弃用,您可以使用 UIAlertcontroller 代替它。 Link:

您需要全局创建 UIAlertView 对象。

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