我需要使用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,但是解决方案是在Swift中,而不是在Objective-c中。
谢谢
我认为在您的情况下,最简单的方法是使用以下内容替换您的最后一个代码行
[UIApplication.sharedApplication.windows.lastObject.rootViewController
presentViewController:alertVC animated:YES completion:nil];
+(UIViewController *)returnTopView {
UIWindow *keyWindow = nil;
NSArray *mulWindows = [[UIApplication sharedApplication]windows];
for (UIWindow *window in mulWindows) {
if (window.isKeyWindow) {
keyWindow = window;
break;
}
}
return keyWindow.rootViewController;
}
使用此控制器在应用内显示警报。试试这个,希望对您有所帮助。
问题是,在iOS 13中,此表达式...
[[UIApplication sharedApplication] delegate].window.rootViewController
...是nil
。这是因为window属性属于scene委托,而不是app委托。您最好通过UIApplication itself访问窗口。例如,浏览应用程序的窗口并找到关键的窗口。