UIAlertController自iOS 13起消失

问题描述 投票:0回答:1

我具有以下功能,该功能会弹出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,我的警报在关闭前弹出不到一秒钟。

发生了什么变化才能使这种情况发生?预先感谢。

objective-c uialertview uialertcontroller ios13
1个回答
0
投票
[似乎已经改变的是,在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; [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 *)UIApplication.sharedApplication).alertWindow setHidden:YES]; }]; UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self saveHapticSettingOff]; [((AppDelegate *)UIApplication.sharedApplication).alertWindow setHidden:YES]; }]; [alert addAction:offAction]; [alert addAction:onAction]; [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil]; }

对于Swift代码,请检查this answer
© www.soinside.com 2019 - 2024. All rights reserved.