在目标-c中隐藏UIAlertView

时间:2017-05-03 10:43:44

标签: objective-c uialertview

我有两种方法可以出现并消失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];

3 个答案:

答案 0 :(得分:3)

您需要全局创建 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];
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

@steven你不需要创建任何解除警报的方法。但我认为你也检查下面的链接,因为UIAlertview已被弃用,你可以使用UIAlertcontroller而不是它。 链接: UIAlertView first deprecated IOS 9