首页 > 解决方案 > 在 iOS 13 [UIApplication sharedApplication] delegate].window 中使用objective-c 不起作用

问题描述

我需要使用 Objective-c 显示来自非 UIView 类的警报。

下面是代码:

 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@“Sample” message message:@“hello message” preferredStyle:UIAlertControllerStyleAlert];

[alertVC addAction:[UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}]];

[[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:alertVC animated:YES completion:nil];

上面的这个不再起作用了,对于objective-c中的相同代码,我无法找到任何其他替代方法。我遇到了这个链接How to resolve: 'keyWindow' was deprecated in iOS 13.0 but solution is in Swift, not in objective-c 。

谢谢

标签: objective-cios13uialertcontrolleruiwindow

解决方案


问题是在 iOS 13 中,这个表达式......

[[UIApplication sharedApplication] delegate].window.rootViewController

...是nil。那是因为 window 属性属于场景委托,而不是应用委托。您最好通过 UIApplication本身来访问窗口。例如,查看应用程序的窗口并找到关键的窗口。


推荐阅读