Can I appear in the Specific ViewController?

I am using a navigation based application. I put the First ViewController in the Second ViewController and from the Second ViewController in the Third ViewController. Now I want to switch from the third ViewController to the First ViewController. I am performing this task using the code below, but my application crashed.

Please, any body, give me the correct instructions. I cannot use pop to rootViewController because it is a different viewController. Thank you in advance...

In the third ViewControler, I wrote this:

FirstViewCtr *x=[[FirstViewCtr alloc] initWithNibName:@"FirstViewCtr" bundle:nil]; [self.navigationController popToViewController:x animated:NO]; 
+47
iphone uinavigationcontroller
Jun 12 '10 at 5:05 a.m.
source share
16 answers

By writing the first line, you get the Indices of all view controllers from the second line as well. You will reach your destination.

 NSArray *array = [self.navigationController viewControllers]; [self.navigationController popToViewController:[array objectAtIndex:2] animated:YES]; 
+93
Jun 12 '10 at 9:15
source share

A safer approach:

 - (void)turnBackToAnOldViewController{ for (UIViewController *controller in self.navigationController.viewControllers) { //Do not forget to import AnOldViewController.h if ([controller isKindOfClass:[AnOldViewController class]]) { [self.navigationController popToViewController:controller animated:YES]; return; } } } 
+75
Feb 21 '13 at 13:12
source share

Timely way:

  let dashboardVC = navigationController!.viewControllers.filter { $0 is YourViewController }.first! navigationController!.popToViewController(dashboardVC, animated: true) 
+15
Dec 21 '15 at 13:59 on
source share
 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]; 
+7
May 28 '16 at 6:33
source share

It is often important to do this on top of the stack, therefore:

 - (void)popToLast:(Class)aClass { for (int i=self.navigationController.viewControllers.count-1; i>=0; i--) { UIViewController *vc = self.navigationController.viewControllers[i]; if ([vc isKindOfClass:aClass]) { [self.navigationController popToViewController:vc animated:YES]; break; } } } 

and you call it

 popToLast:[SomeViewController class]; 
+6
Sep 04 '13 at 9:57 on
source share
 - (void) RetunToSpecificViewController{ for (UIViewController *controller in self.navigationController.viewControllers) { if ([controller isKindOfClass:[AnOldViewController class]]) { //Do not forget to import AnOldViewController.h [self.navigationController popToViewController:controller animated:YES]; break; } } } 
+5
Apr 28 '15 at 7:41
source share

Fast and safe version of Swift 3 :

 if let vc = navigationController.viewControllers.filter({ $0 is SpecificViewControllerClass }).first { navigationController.popToViewController(vc, animated: true) } 
+2
Jun 07 '17 at 15:06
source share

Your code creates a new instance of the view that has never been pushed onto the stack, then tries to return to that controller.

If you return to the root view controller, you can use popToRootViewControllerAnimated:

If you select a known distance, you can call popViewControllerAnimated: more than once. In your example, it will be 2 controllers, therefore for calls. You can do the same by looking at viewControllers for controller 2 from the end and swimming towards it.

The above suggestions are quick fixes. One of the most effective scenarios will be the transition of the controller that you want to return to each subsequent controller that you click. First, he goes to the second, and the second sends a link to the third, third premise of the link passed, which is the first.

Essentially, you are creating a temporary root controller. You can subclass the UINavigationController and add the temporaryRoot property and the popToTemporaryRootViewControllerAnimated: method, which will appear on your temporary root and clear it. When you press seconds for the first time, it also sets itself as a temporary root, so each controller on the stack should not pass the link around. You will need to add additional checks to prevent you from ever missing a temporary download without clearing it.

+1
Jun 12 2018-10-12T00:
source share

After many efforts, someone created a quick extension back to a specific view controller in Swift 3.0.

 extension UINavigationController { func backToViewController(viewController: Swift.AnyClass) { for element in viewControllers as Array { if element.isKind(of: viewController) { self.popToViewController(element, animated: true) break } } } } 

Method call:

  self.navigationController?.backToViewController(viewController: YourViewController.self) 
+1
May 08 '17 at 12:17
source share

Implemented and tested in Swift 3.0

The following is a method that can be useful for navigating to any particular View controller:

 func poptoSpecificVC(viewController : Swift.AnyClass){ let viewControllers: [UIViewController] = self.navigationController!.viewControllers for aViewController in viewControllers { if aViewController.isKind(of: viewController) { self.navigationController!.popToViewController(aViewController, animated: true) break; } } } 

Using:

 self.poptoSpecificVC(viewController: createIntervalVC.self) 
+1
Jul 17 '17 at 14:15
source share

I think .filter({...}).first bit slower than .first(where: {...}) . It can also be written more precisely to address only UIViewControllers.

 extension UINavigationController { func popToController<T: UIViewController>(_ type: T.Type, animated: Bool) { if let vc = viewControllers.first(where: { $0 is T }) { popToViewController(vc, animated: animated) } } func popToControllerOrToRootControllerIfNotInTheStack<T: UIViewController>(_ type: T.Type, animated: Bool) { if let vc = viewControllers.first(where: { $0 is T }) { popToViewController(vc, animated: animated) } else { popToRootViewController(animated: animated) } } } 
+1
Oct 24 '17 at 11:46 on
source share

Updated for Swift 3:

used below simple code, for pop for a specific view controller;

  for vc in self.navigationController!.viewControllers as Array { if vc.isKind(of: YourViewControllerName) { self.navigationController!.popToViewController(vc, animated: true) break } } 
+1
Nov 16 '17 at 4:47 on
source share

Swift 4 version

 if let viewController = navigationController?.viewControllers.first(where: {$0 is YourViewController}) { navigationController?.popToViewController(viewController, animated: false) } 

You can specify a different filter in .viewControllers.first according to your needs, for example, say, if you have the same kind view controllers located in the navigation controller, then you can specify an additional check, as shown below

  if let viewController = navigationController?.viewControllers.first(where: { if let current = $0 as? YourViewController { return current.someProperty == "SOME VALUE" } return false } ) { navigationController?.popToViewController(viewController, animated: false) } 
+1
Aug 01 '18 at 20:15
source share
 for controller in self.navigationController!.viewControllers as Array { if controller.isKind(of: LoginVC.self) { _ = self.navigationController!.popToViewController(controller, animated: true) break } } 
0
Mar 09 '18 at 15:54
source share

Put the function in UIViewController 1. It checks if the Specific UIViewController in the UINavigationController , then popToViewController or another pushViewController

 func navigate(_ navVC: AnyClass, pushVC: UIViewController) { for obj in self.navigationController!.viewControllers { if obj.isMember(of: navVC) { self.navigationController!.popToViewController(obj, animated: true) return } } self.navigationController!.pushViewController(pushVC, animated: true) } 

Using

 self.navigate(ViewController.self, pushVC: self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController) 
0
May 15 '18 at 7:36
source share

I have an answer here. This is 100% working code for Swift> 4.X

How can I insert a specific View Controller in Swift

0
Jul 30 '19 at 6:07
source share



All Articles