【问题标题】:Presenting an alert from a view controller that has closed从已关闭的视图控制器中显示警报
【发布时间】: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!

谁能给点建议?

【问题讨论】:

标签: ios objective-c uiviewcontroller uialertcontroller


【解决方案1】:

如果您想在关闭视图控制器时显示警报,那么您可以使用完成块实现解除并在那里显示它。例如:

[self dismissViewControllerAnimated:YES completion:^(void) {
  // UIAlertController code here

}];

【讨论】:

    【解决方案2】:

    您可以将 UIWindow 与透明的 UIViewController 一起使用,然后在其上显示 UIAlertController。 下面的链接提供了 UIAlertController 上的一个类别,该类别在 Objective-C 中有一个 show 方法。你可以使用它。

    链接:https://stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-when-not-in-a-view-controller

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多