【问题标题】:Dismissing a view controller and opening another one once complete, giving error关闭视图控制器并在完成后打开另一个视图控制器,出现错误
【发布时间】:2017-11-19 10:46:27
【问题描述】:

我处理了一些照片,完成后,我调用以下函数来显示输出 UI。我需要确保当前的捕获视图控制器被解除并且不希望它在后台运行。我使用的功能是这样的:

-(void)openOutputUIWithImage:(UIImage*)displayImage{ //update UI with new viewcontroller with specified image to display
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    outputView = [mainStoryboard instantiateViewControllerWithIdentifier:@"OutputView"];
    outputView.img = displayImage;
    [self dismissViewControllerAnimated:NO completion:^{
        [self presentViewController:outputView animated:YES completion:^{
            NSLog(@"output view opened!");
        }];
    }];
}

但我收到以下错误:

Warning: Attempt to present <OutputViewController: 0x100858af0> on <CircleDetectionViewController: 0x100857c20> whose view is not in the window hierarchy!

我真的不确定我做错了什么,我没有太多使用多个视图控制器的经验。它似乎在我的另一个视图控制器中点击了我的 viewDidLoad 方法,但只显示了一个空白屏幕。任何帮助将非常感激!提前致谢!

【问题讨论】:

    标签: ios objective-c uiviewcontroller uistoryboard


    【解决方案1】:

    这行得通:

    UIViewController* presentingViewController = self.presentingViewController;
        [self dismissViewControllerAnimated:YES completion:^{
            [presentingViewController presentViewController:outputView animated:YES completion:nil];
    
        }];
    

    引用了这个答案:https://stackoverflow.com/a/33058529/5071756

    【讨论】:

      【解决方案2】:

      此代码中的self 是谁?根据错误,self 是您的CircleDetectionViewController。这意味着这里发生的事情是dismissViewControllerAnimated 导致调用 VC 自行关闭(因此它不再位于视图层次结构中,然后尝试在其自身之上呈现一个视图控制器,即使它刚刚被关闭。

      想到了 2 个可能的修复方法:

      1. 将您的 OutputViewController 显示在您的 CircleDetectionViewController 之上,而不要先将其关闭。

      2. 无论父视图控制器是什么,处理在这些呈现的视图控制器之外关闭/呈现的逻辑。

      【讨论】:

      • 有没有办法从当前的视图控制器中解散前一个视图控制器?
      【解决方案3】:

      首先不必要地分配OutputViewController 并再次从情节提要中获取OutputViewController。所以OutputViewController* outputView = [[OutputViewController alloc] init]; 不需要代码行。

      您正试图在CircleDetectionViewController dismissViewController 方法的完成块内打开一个OutputViewController。您正试图在CircleDetectionViewController 之上打开OutputViewController。但是您解雇 CircleDetectionViewController 是错误的。

      获取CircleDetectionViewController 的前一个视图控制器并从中呈现OutputViewController 并关闭CircleDetectionViewController

      更新 1:

      首先呈现输出视图

      [self.presentingViewController presentViewController:outputView animated:YES completion:nil];
      

      并关闭 CircleDetectionView

      [self dismissViewControllerAnimated:NO completion:nil];
      

      【讨论】:

      • 我将对其进行编辑,抱歉我正在使用 init 进行测试,以前从未出现过。
      • 有没有办法从之前的视图控制器中呈现视图控制器,比如[PreviousViewController presentViewController:outputView animated:YES completion:nil];
      • 或者我可以使用 NSNotification 来触发先前视图控制器中的方法来实例化新的吗?
      • 我得到了相同的空白屏幕结果,但该错误已消失。我得到以下信息:This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
      • 对不起,把self.presentedViewController改成self.presentingViewController
      猜你喜欢
      • 2017-02-03
      • 2014-12-12
      • 1970-01-01
      • 2016-11-20
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多