【问题标题】:UIAlertController disappearing since iOS 13UIAlertController 自 iOS 13 起消失
【发布时间】:2019-09-28 16:37:11
【问题描述】:

我有以下功能,它会弹出一个 UIAlert,允许用户更新他们的触觉反馈设置:

- (void)requestHapticSetting{
    UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alertWindow.rootViewController = [[UIViewController alloc] init];
    alertWindow.windowLevel = UIWindowLevelAlert + 1;
    [alertWindow makeKeyAndVisible];

    if(isHapticOn){
        hapticMessage = @"Haptic feedback is currently\nturned ON.\nPlease update preference.";
    }
    else {
        hapticMessage = @"Haptic feedback is currently\nturned OFF.\nPlease update preference.";
    }

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Haptic Setting"
                                                                   message:hapticMessage
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* onAction = [UIAlertAction actionWithTitle:@"TURN ON" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         [self saveHapticSettingOn];
                                                     }];

    UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {
                                                          [self saveHapticSettingOff];
                                                      }];
    [alert addAction:offAction];
    [alert addAction:onAction];
    [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}

我已经用了几年了,效果很好。

但是,自从更新到 iOS 13 并更新到最新的 Xcode 后,我的警报在关闭前弹出了不到一秒钟。

发生了哪些变化?提前致谢。

【问题讨论】:

  • 这本质上是 stackoverflow.com/questions/58131996/… 的副本,但这是在 Swift 中,当您支持场景时,它的答案在 iOS 13 下将无法正常工作。
  • 您是在 iOS 13 下的应用中使用场景,还是选择完全退出场景(您不应该这样做)?
  • @rmaddy 我不完全确定。我使用生成 Xcode 项目的游戏引擎。不幸的是,在过去添加这个一直有效到现在。谢谢。

标签: objective-c uialertview uialertcontroller ios13


【解决方案1】:

似乎发生了变化,在 iOS 12 和以前的版本中,您的应用只需调用 [alertWindow makeKeyAndVisible]; 即可对窗口进行强引用,而在 iOS 13 中则不再存在。

发生的情况是,对您的 alertWindow 的唯一强引用是在您的 requestHapticSetting func 中,并且一旦此 func 返回,窗口就会被销毁,从而从视图中删除您的警报。

这可能只是通过采用 iOS 13 场景来解决,但我还没有测试过。我可以建议,如果您使用场景 将无法正常工作,将警报窗口保存在代码中某处的强变量中,然后使用它来显示警报。我建议在单例或 AppDelegate 本身中这样做。

//AppDelegate.h
...
@property (strong) UIWindow *alertWindow;
....

//AppDelegate.m
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    self.alertWindow = [[UIWindow alloc] init];
    self.alertWindow.rootViewController = [[UIViewController alloc] init];
    self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
    ...
}
...

//Your class that's presenting the alert
#import "AppDelegate.h"
...
- (void)requestHapticSetting{
    AppDelegate *appDelegate = (AppDelegate *)UIApplication.sharedApplication;
    [appDelegate.alertWindow makeKeyAndVisible];
    if(isHapticOn){
        hapticMessage = @"Haptic feedback is currently\nturned ON.\nPlease update preference.";
    } else {
        hapticMessage = @"Haptic feedback is currently\nturned OFF.\nPlease update preference.";
    }

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Haptic Setting"
                                                               message:hapticMessage
                                                        preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* onAction = [UIAlertAction actionWithTitle:@"TURN ON" style:UIAlertActionStyleDefault
                                                 handler:^(UIAlertAction * action) {
                                                      [self saveHapticSettingOn];
                                                      [appDelegate.alertWindow setHidden:YES];
                                                 }];

    UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * action) {
                                                      [self saveHapticSettingOff];
                                                      [appDelegate.alertWindow setHidden:YES];
                                                  }];
    [alert addAction:offAction];
    [alert addAction:onAction];
    [appDelegate.alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}

对于 Swift 代码,请查看this answer

【讨论】:

    【解决方案2】:

    我创建了一个辅助类,它支持新的 UIWindowScene 和 iOS 13.X 和 swift 5.X。大家可以试试

    https://github.com/emraz/ERAlertController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多