Rea*_*ion 3 objective-c uialertview uialertcontroller ios13
我有以下功能会弹出一个 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];
}
Run Code Online (Sandbox Code Playgroud)
我已经用了几年了,效果很好。
但是,自从更新到 iOS 13 并更新到最新的 Xcode,我的警报在关闭前弹出不到一秒钟。
发生了什么变化可能会导致这种情况发生?提前致谢。
似乎发生了变化的是,在 iOS 12 和以前的版本中,您的应用程序只需调用 即可持有对窗口的强引用[alertWindow makeKeyAndVisible];,而在 iOS 13 中则不再如此。
发生的事情是对您的唯一强引用alertWindow在您的requestHapticSettingfunc 中,并且一旦此 func 返回,窗口就会被销毁,从而从视图中删除您的警报。
这可能仅通过采用 iOS 13 场景就可以解决,但我尚未对此进行测试。我可以建议,如果您使用场景将无法正常工作,将您的警报窗口放在代码中某处的强变量中,然后使用它来显示警报。我建议在单例或 AppDelegate 本身中这样做。
//AppDelegate.h
...
@property (strong) UIWindow *alertWindow;
....
Run Code Online (Sandbox Code Playgroud)
//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;
...
}
...
Run Code Online (Sandbox Code Playgroud)
//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];
}
Run Code Online (Sandbox Code Playgroud)
对于 Swift 代码,请查看此答案。
| 归档时间: |
|
| 查看次数: |
4101 次 |
| 最近记录: |