gpt4 book ai didi

objective-c - UIAlertController 自 iOS 13 起消失

转载 作者:行者123 更新时间:2023-12-04 00:02:28 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

这可能只是通过采用 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 .

关于objective-c - UIAlertController 自 iOS 13 起消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58148334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com