【问题标题】:Dismiss and Pop a view controller关闭并弹出视图控制器
【发布时间】:2014-12-17 22:23:07
【问题描述】:

我想在我的视图控制器堆栈上返回两个级别。我按此顺序设置了三个序列:Show、Show、Present Modally。有一个导航控制器正在使用中。从我的第四个视图我想回到第二个视图。我试过使用 self.presentingViewController?.presentingViewController?.navigationController?.popViewControllerAnimated(false);

和

self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(false, completion: nil);

第二个仅在第二个和第三个连续为“当前模态”时才有效。我怎样才能让他们与解雇和流行一起工作?

【问题讨论】:

  • 似乎你必须在弹出其他两个之前关闭呈现的视图控制器。
  • 你可以使用 unwind segue 代替。
  • @rdelmar 如果您使用情节提要,您只能使用展开转场,对吧?
  • @LyndseyScott,没错。

标签: ios swift uiviewcontroller popviewcontrolleranimated


【解决方案1】:

尝试在弹出其他两个视图控制器之前关闭呈现的视图控制器:

func dismissThenDoublePop() {

    // Get the presenting/previous view
    let previousView = self.presentingViewController as UINavigationController

    // Dismiss the current view controller then pop the others
    // upon completion
    self.dismissViewControllerAnimated(true, completion:  {

        // Get an array of the current view controllers on your nav stack
        let viewControllers: [UIViewController] = previousView.viewControllers as [UIViewController];

        // Then either pop two view controllers, i.e. pop
        // to viewControllers[viewControllers.count - 2], or
        // pop to the second view controller in the nav stack,
        // i.e. viewControllers[1]. (In this case I've used the
        // first option.)
        self.navigationController!.popToViewController(viewControllers[viewControllers.count - 2], animated: true);

    });
}

【讨论】:

  • 它到达以let viewControllers:... 开头的行并因“致命错误:在展开可选值时意外发现 nil”而崩溃
  • @Ricca 是的,你说得对,导航控制器在呈现的视图控制器中为零。现在正在寻找替代方案。
  • @Ricca 好的,我已经更新了我的答案,以便在 dismissView 块之前将上一个视图作为 UINavigationController 获取。我已经测试了这段代码,它对我有用。
  • @LyndseyScott 我尝试以这种方式移动到主屏幕。你能检查一下它是否正确吗?让 previousView = self.presentingViewController 为! UINavigationController self.dismiss(animated: true, completion: { // 获取导航堆栈上当前视图控制器的数组 let viewControllers: [UIViewController] = previousView.viewControllers as [UIViewController]; previousView.popToViewController(viewControllers[0],动画:真);});
【解决方案2】:

你可以使用这个技巧

https://stackoverflow.com/a/15839298/1153100

简单而干净(展开转场)

@IBAction func unwindAction(segue: UIStoryboardSegue) {
}

【讨论】:

    【解决方案3】:

    你用这样的。

    self.navigationController?.popViewControllerAnimated(true)
    

    【讨论】:

      【解决方案4】:

      只需调用 dismissViewControllerAnimated。它会自动关闭所有视图控制器,包括呈现的模型视图控制器。

      目标 - C

      [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 
      

      斯威夫特

      self.navigationController?.dismissViewControllerAnimated(false, completion: nil);
      

      【讨论】:

        猜你喜欢
        • 2014-08-27
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        相关资源
        最近更新 更多