Dismiss and popViewController not working when presentingViewController is UITabbarController

145 Views Asked by At

My scence

UITabbar ( UINavigation(page1) -push-> page2 -push-> page3 -present-> page4 )

From my feature I stay on page4 and want to dismiss it and pop to root ( page1 ) but when I get presentingViewController it return UITabbarController not my UINavigationBarController

My code now.

self.dismiss(animated: true) { [weak self] in
    guard let topVc = self?.presentingViewController else { return }
    if let destinationVC = topVc.navigationController?.viewControllers.filter({$0 is Page1ViewController}).first {
        topVc.navigationController?.popToViewController(destinationVC, animated: true)
    }
}

My code working before I add UITabbar to my flow.

Can someone help me or explain me about root cause ? Thank you for help.

Updated Now I found a solution about this but I think it not the good way enough.

self.viewController.dismiss(animated: true) {
    guard let topVc = Tools.getTopViewController(),
        let tabBarVc = topVc as? MainTabBarController,
        let nav = tabBarVc.selectedViewController as? BaseNavigationBarController else { return }
    if let destinationVC = nav.viewControllers.filter({$0 is WalletViewController}).first {
        nav.popToViewController(destinationVC, animated: true)
    }
}
0

There are 0 best solutions below