【发布时间】:2021-09-24 16:58:02
【问题描述】:
我正在尝试用 UIAlertController 替换对已弃用 UIAlertView 的调用。显示警报的类不是视图控制器,所以我不确定用什么来代替“self presentViewController”。
对于背景,这是我使用 UIAlertView 显示警报的方式:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
这是我打算使用 UIAlertController 替换的:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
但是,如前所述,我不能从这个位置使用“self presentViewController”,所以我尝试像这样获取(并使用)视图控制器:
UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[viewController presetViewController:alert animated:YES completion:nil];
上面的代码导致编译错误,“No visible @interface for 'UIViewController' declarations selector 'presetViewController:animated:completion:'”。
我应该如何解决这个问题?
谢谢!
PS:不用我说很明显,但我是一个 Objective-C 新手。我花了 20 多年的时间在其他操作系统上编写 c++……
【问题讨论】:
标签: objective-c uialertview uialertcontroller presentviewcontroller