【问题标题】:Hide UIAlertView In objective -c在目标中隐藏 UIAlertView -c
【发布时间】:2017-05-03 10:43:44
【问题描述】:

我有两种方法可以让 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];
}

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

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

【问题讨论】:

  • 您是否要在不选择Yes或No的情况下自动关闭alertview?请简洁明了
  • UIAlertView 已弃用。请参阅this answer 获取最新示例。

标签: objective-c uialertview


【解决方案1】:

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

【讨论】:

  • 非常感谢@Nirmalsinh。
【解决方案2】:

不推荐使用 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];
}

【讨论】:

    【解决方案3】:

    @steven 你不需要创建任何关闭警报的方法。但我认为您还检查了下面的链接,因为 UIAlertview 已被弃用,您可以使用 UIAlertcontroller 代替它。 关联: UIAlertView first deprecated IOS 9

    【讨论】:

    • 苹果在 OS 9 之后允许使用它。所以现在我们可以再次使用它。
    猜你喜欢
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 2018-06-04
    相关资源
    最近更新 更多