How to Pop Specific View Controller in Swift

How can I pop specific View Controller in Swift

Try following code:

for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}

Can i pop to Specific ViewController?

By Writing the First Line you get the Indexes of all View Controllers and from second Line You will reach up to your Destination.

NSArray *array = [self.navigationController viewControllers];

[self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];

Swift & Navigation : How do I pop until a certain ViewController?

The navigation controller holds a group of view controller and you can pop to any sub-controller you want. For example your flow seem like:

HomeViewController -> ContactViewController -> ContactDetailsViewController -> ChatViewController

Then from ChatViewController you want to push back to ContactViewController:

class ChatViewController: UIViewController {
....
func popToContact() {
if let contactVC = self.navigationController?.viewControllers.filter { $0 is ContactViewController }.first {
self.navigationController?.popToViewController(contactVC, animated: true)
}
}
}

Back to specific view controller in swift 4

Add delegate in FourthViewController:

self.dismiss(animated: true) {
self.delegate.popToFirstVC()
}

Add func popToFirstVC() in ThirdViewController.
Use popToViewController:

func popToFirstVC() {
if let firstViewController = self.navigationController?.viewControllers[1] {
self.navigationController?.popToViewController(firstViewController, animated: true)
}
}

or better

guard let viewControllers = self.navigationController?.viewControllers else {
return
}

for firstViewController in viewControllers {
if firstViewController is FirstViewController {
self.navigationController?.popToViewController(firstViewController, animated: true)
break
}
}

There is still such an option.
Add an Observer for this function and call where necessary.
But I would do it only in the most extreme cases.

func popToThisVC() {
if let topController = UIApplication.topViewController() {
topController.navigationController?.popToViewController(self, animated: true)
}
}

Swift 4 pop to a view controller : navigation method

Make sure your controller is in the navigation stack and you can try this code.

for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: SOListScreen .self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}

Popping to specific ViewController in NavigationController stack

I tried your approach, and it really doesn't work.

You should remove LearnViewController from your navigation stack in LearnResultViewController.

navigationController?.viewControllers.removeAll(where: { $0 is LearnViewController })

After that, when you hit the back button, you will get back to VocabularyViewController. And the back button will be there.

You shouldn't override willMove for that.

How to pop view controller to one of the previous view controllers in swift?

Instead of doing a generic popViewControllerAnimated: call, use popToViewController:animated:. You could detect if the user has deleted all of the rows in which case, do something like this (otherwise just pop one view controller):

let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 2], animated: true);

Pop 2 view controllers in Nav Controller in Swift

Expanding on my comment, find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.

Example (assumes the navigation controller has more than 1 view controller):

func backTwo() {
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
}

Objective-C

NSArray *viewControllers = [self.navigationController viewControllers];
[self.navigationController popToViewController:viewControllers[viewControllers.count - 3] animated:YES];


Related Topics



Leave a reply



Submit