0

TLDR: I need to manipulate a different navigation stack than what the user is currently on, so that when the different stack is shown, it's always on it's root view controller.

I am working on a SWIFT e-commerce project where the user can at any point tap a cart BarButtonItem to pop-up a view controller that shows their shopping cart. This cart is shown using the code below:

let cartController: CartNavigationController = self.storyboard!.instantiateViewController()
    self.navigationController!.present(cartController, animated: true, completion: nil)

There is a certain special product that is key to my logic on a specific tabBarItem navigation stack. I want to make sure that if the user deletes this product from their cart, that this specific tabBarItem navigation stack is at it's root view controller when the cart view controller is dismissed.

I know this can be achieved pretty easily by passing a variable between two view controllers. The problem is that I have no way of knowing which viewcontroller accessed the cart, or even which tab the user is currently on, as the cart is accessible from all view controllers.

I've tried a bunch of methods here, but none of them work. Trying to use the completion handler on the self.dismiss() doesn't seem to do anything. I've tried accessing presentingViewController and presentedViewController to manipulate the destination stack after dismissal, but with no luck yet.

Hopefully that made sense. Is this possible? Thanks.

Alex Ritter
  • 1,009
  • 3
  • 18
  • 40
  • Have you tried a delegate pattern? If you assign the root view controller a delegate you should be able to navigate back to where you want to be – Jake Aug 11 '19 at 11:07
  • It would work for sure, but I'd need to make every view controller conform to that custom protocol, wouldn't I? I was hoping to find a simpler solution :( It might not exist. – Alex Ritter Aug 11 '19 at 11:28
  • I think you could create an extension on UiviewController that adheres to the protocol – Jake Aug 11 '19 at 11:47
  • I'd need to duplicate the extension to conform to UITableViewController as UICollectionViewController too. – Alex Ritter Aug 11 '19 at 13:08
  • Oh I thought those all inherited from uiviewcontroller. – Jake Aug 11 '19 at 13:09
  • I’ve always implemented the table views and collection views manually so I never use those classes – Jake Aug 11 '19 at 13:10
  • Yea, I'm feeling that's the way to go too after this project. – Alex Ritter Aug 11 '19 at 13:19
  • If someone thinks of a better way to do this, I'm all ears. I decided to just do a check for the "special product" in viewDidAppear on every VC. Not efficient, but it gets the job done. – Alex Ritter Aug 12 '19 at 14:52

0 Answers0