UIAlertController在iOS 13中消失了

3
我有以下函数,它会弹出一个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之后,我的警告框弹出不到一秒钟就会关闭。

是什么改变导致这种情况发生?提前感谢。


这本质上是 https://dev59.com/AFMH5IYBdhLWcg3w0Tbf 的重复,但那是使用 Swift 编写的,而且在支持场景 (scenes) 的 iOS 13 下其答案不能正常工作。 - rmaddy
你在iOS 13的应用程序中使用场景了吗?还是选择完全退出场景(这是不应该做的)? - rmaddy
@rmaddy 我不是完全确定。我使用一个生成Xcode项目的游戏引擎。不幸的是,直到现在为止,过去添加这个一直都有效。谢谢。 - Reanimation
2个回答

7
在iOS 12及以前的版本中,只需调用[alertWindow makeKeyAndVisible];就可以使你的应用程序对窗口保持强引用,但在iOS 13中不再如此。现在,你的alertWindow唯一的强引用在你的requestHapticSetting函数里。一旦该函数返回,窗口就会被销毁,因此从视图中移除你的警告。这可能可以通过采用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代码,请参阅此答案


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接