【发布时间】:2017-07-27 23:06:45
【问题描述】:
我有一个 iOS Objective-C 应用程序,我试图从正在关闭的 UIViewController 中显示 UIAlertController。我已经尝试在AppDelegate 中添加这个常见的解决方法:
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
return topVC;
}
调用:
[appDelegate.currentTopViewController presentViewController:alert animated:YES completion:nil];
但是这个错误仍然出现:
Warning: Attempt to present UIAlertController on MyViewController whose view is not in the window hierarchy!
谁能给点建议?
【问题讨论】:
-
您应该向父视图控制器发送通知(或使用委托等)并从那里呈现
-
@nathan 你能提供一个使用通知的例子吗?
-
委托示例:stackoverflow.com/a/33229483/2124535,NotificationCenter 示例:stackoverflow.com/a/29738348/2124535
标签: ios objective-c uiviewcontroller uialertcontroller