找到你要的答案

Q:Dismiss and Pop a view controller

Q:解散和弹出一个视图控制器

I want to go back two levels on my view controller stack. I have three segues in this order: Show, Show, Present Modally. There is a navigation controller in use. From my 4th view I want to go back to the 2nd view. I have tried using self.presentingViewController?.presentingViewController?.navigationController?.popViewControllerAnimated(false);

and

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

The second one works only if the 2nd and 3rd segues as 'Present Modally'. How can I get them to work with a dismiss and a pop?

I want to go back two levels on my view controller stack. I have three segues in this order: Show, Show, Present Modally. There is a navigation controller in use. From my 4th view I want to go back to the 2nd view. I have tried using self.presentingViewController?.presentingViewController?.navigationController?.popViewControllerAnimated(false);

self.presentingViewController?。presentingviewcontroller?。dismissviewcontrolleranimated(假,完成:无);

The second one works only if the 2nd 和 3rd segues as 'Present Modally'. How can I get them to work with a dismiss 和 a pop?

answer1: 回答1:

Try dismissing the presented view controller before popping the other two:

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);

    });
}

试着去呈现的视图控制器在弹出的其他两:

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);

    });
}
answer2: 回答2:

You can use this technique

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

Simple and clean(Unwind segues)

@IBAction func unwindAction(segue: UIStoryboardSegue) {
}

你可以使用这个技术

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

Simple 和 clean(Unwind segues)

@IBAction func unwindAction(segue: UIStoryboardSegue) {
}
answer3: 回答3:

You use like this one.

self.navigationController?.popViewControllerAnimated(true)

你像这样使用。

self.navigationController?.popViewControllerAnimated(true)
ios  swift  uiviewcontroller  popviewcontrolleranimated