0

I have three controllers and one swiftUI view, below is the flow

A Controller(RootController) -> B controller - > C controller -> SwiftUI view

I am doing some operation on the SwiftUI view based on the operation need to decide to whether pop back to C controller or B. Currently I am able pop back to C Controller and root controller, not sure how to jump to specific (in my case B controller)controller in SwiftUI

.navigationBarItems(leading:Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        }) 

I mean to say instead of this line self.presentationMode.wrappedValue.dismiss() is there any method to pass view controller to pop back

OhStack
  • 106
  • 1
  • 1
  • 15
  • Does this answer your question? [Come back to a specific View in SwiftUI (popToViewController)](https://stackoverflow.com/questions/57700532/come-back-to-a-specific-view-in-swiftui-poptoviewcontroller) – Gyani Aug 19 '22 at 12:12
  • Does this answer your question? [How can I pop to the Root view using SwiftUI?](https://stackoverflow.com/questions/57334455/how-can-i-pop-to-the-root-view-using-swiftui) – Sachin Singh Aug 19 '22 at 12:18
  • @SachinSingh I am able pop to root view and previous not the second one which is in-between – OhStack Aug 19 '22 at 12:38
  • @Asperi , not serving my purpose – OhStack Aug 19 '22 at 12:40
  • Needed minimal reproducible example. – Asperi Aug 19 '22 at 12:41
  • @Asperi, is possible to pass controller, check below code .navigationBarItems(leading:Button(action: { if conditionSatisfy{ self.presentationMode.wrappedValue.dismiss() }else{ popTo B controller } }) – OhStack Aug 19 '22 at 14:09

2 Answers2

1

You can make an extension for UINavigationController

extension UINavigationController {
  func popToViewController(ofClass: AnyClass, animated: Bool = true) {
    if let vc = viewControllers.last(where: { $0.isKind(of: ofClass) }) {
      popToViewController(vc, animated: animated)
    }
  }
}

and use it like this

 func goBack() {
        navigationController?.popToViewController(ofClass:  UIHostingController<SomeSwinftUIView>.self) // if this is a swuftUI view
}

or

 func goBack() {
    navigationController?.popToViewController(ofClass: SomeViewController.self)
    
    }
Marta Paniti
  • 152
  • 6
  • how to use UINavigationController inside SwiftUI, tried like below seems Cannot find 'navigationController' in scope .navigationBarItems(leading: Button(action: { navigationController?.popToViewController(ofClass: SomeViewController.self) }) – OhStack Aug 19 '22 at 14:13
  • just import UIKit (Personally i'm using a router/coordinator for navigation, and there I include UIKit) – Marta Paniti Aug 20 '22 at 16:02
1

@OhStack just import UIKit (Personally i'm using a router/coordinator for navigation, and there I include UIKit)

import UIKit
import SwiftUI

struct SwiftUIViewCoordinator {
     let navController: UINavigationController?
     
     func startView(navController: UINavigationController) {
           self.navController = navController
           let swiftUIView = SwifUIViewView(router: self)
           let hostingView = UIHostingController(rootView: swiftUIView)
           navigationController?.pushViewController(hostingView, animated: true)
     }

     func goBackToSomeView() {
        navigationController?.popToViewController(ofClass:  
        UIHostingController<SomeSwinftUIView>.self) 
      }
}
Marta Paniti
  • 152
  • 6