【问题标题】:Dont dismiss UIAlertController不要关闭 UIAlertController
【发布时间】:2016-12-18 12:53:24
【问题描述】:

我目前有一个 UIAlertController,我需要将其设为不可解雇。按下操作按钮时,警报不应关闭。

我该怎么做?

 UIAlertController *alert;

    int bestScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"bestScore"] intValue];
    if (!bestScore || bestScore < _score){
        [[NSUserDefaults standardUserDefaults] setObject:@(_score) forKey:@"bestScore"];
        alert = [UIAlertController alertControllerWithTitle:@"GAME OVER "
                                                    message:[NSString stringWithFormat:@"NEW RECORD! \n SCORE : %d  \n\n\n\n\n\n", _score] preferredStyle:UIAlertControllerStyleActionSheet];
    }
    else   alert = [UIAlertController alertControllerWithTitle:@"GAME OVER"
                                                       message:[NSString stringWithFormat:@"SCORE : %d \n Best score : %d  \n\n\n\n\n\n ", _score, bestScore] preferredStyle:UIAlertControllerStyleAlert];

    [alert addAction:[UIAlertAction actionWithTitle:@"Try again" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self newGame];
        [self addNewView];

    }]];


      [alert addAction:[UIAlertAction actionWithTitle:@"Answer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [alert viewWillDisappear:NO];

            }]];
 [self presentViewController:alert animated:YES completion:nil];

【问题讨论】:

  • 您将需要使用/创建一些其他技术来显示您的信息。只要点击其中一个按钮,警报控制器就会被关闭
  • UIAlertController是系统alert,不能修改,建议写custom alert

标签: ios objective-c uialertview uialertcontroller


【解决方案1】:

我建议创建一个包含 UIView 的 UIViewController。在此 UIView 中,您将能够显示所需的信息并添加所需的自定义按钮操作。

为了使 UIView 看起来像一个模态视图控制器,添加一个带有 UIBlurEffectStyle 的 UIVisualEffectView。

这就像使用您的故事板/xib 创建一个传统的视图控制器一样简单,在界面构建器中添加所需的 UIView,然后链接相关的视图控制器类。完成初始设置和用户界面后,将以下代码添加到 viewDidLoad。此外,您可以在 viewWillAppear 和 viewWillDisappear 中执行所需的动画等。

UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]
UIVisualEffectsView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
[self.view insertSubview:blurView atIndex:0];

【讨论】:

    【解决方案2】:

    你可以这样做

    如果您不想关闭 alertController,那么您可以禁用它的按钮操作,例如

    alert.actions[1].enabled = NO
    

    这将使您的 alertController 不可关闭。

    【讨论】:

      【解决方案3】:

      只需在您希望它不关闭的 alertButton 的操作开始时再次显示 alertContoller,

      [self presentViewController:alertController animated:YES completion:nil];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-09
        • 2017-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多