objective-c - 在 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 。
谢谢
解决方案
问题是在 iOS 13 中,这个表达式......
[[UIApplication sharedApplication] delegate].window.rootViewController
...是nil
。那是因为 window 属性属于场景委托,而不是应用委托。您最好通过 UIApplication本身来访问窗口。例如,查看应用程序的窗口并找到关键的窗口。
推荐阅读
- c# - UseStatusCodePagesWithReExecute 不会重新执行错误页面
- java - 火花工人无法连接到主人
- ios - iPadOS UIToolbar 旋转后位置错误
- apache - 如何 mod_rewrite / 到 /frontend/index.html?
- python - 自定义 Keras 增强层,无法获取批量大小
- ios - React Native 0.64.2/Xcode 12.5/13 beta:使用新构建系统生成多个命令(默认)
- python - Mixin Enums 并检查 Enum 对象是否是该类的子集之一
- php - 我可以在 Laravel 中引用同一个数据库表吗?
- java - 如何使用 Freemarker 模板输出 JSON
- python - 按列子集数据表