Reputation: 2379
My home viewcontroller is Tabbarcontroller
So, I want to first dismiss presented viewcontroller and then I want to pop my previous pushed controller
Here is my navigation flow
From Tabbarviewcontroller
1- let aVC = self.storyboard?.instantiateViewController(withIdentifier: "a") as! OrderListingViewController
self.navigationController?.pushViewController(aVC, animated: true)
From A viewcontroller
2- let bVC = self.storyboard?.instantiateViewController(withIdentifier: "b") as! OrderListingViewController
self.navigationController?.pushViewController(bVC, animated: true)
From B viewcontroller
let cVC = self.storyboard?.instantiateViewController(withIdentifier: "c") as! RejectOrderViewController
cVC.providesPresentationContextTransitionStyle = true
cVC.definesPresentationContext = true
cVC.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext
self.tabBarController?.presentVC(cVC)
so from C Viewcontroller when i dismiss i want to show Tabbarviewcontroller or (A) ViewController
Upvotes: 7
Views: 7283
Reputation: 1
I had to use NotificationCenter to pop the previous controller.
let back_view = Notification.Name("back_view")
//Here's the action to close the actual controller
@IBAction func closeWW(_ sender: Any) {
self.dismiss(animated: true, completion: {
NotificationCenter.default.post(name: self.back_view, object: nil)
})
}
//Now, in the previous controller
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(popView), name: back_view, object: nil)
}
@objc func popView(){
self.navigationController?.popViewController(animated: true)
}
Maybe it's not the best answer, but it's work for me :)
Upvotes: 0
Reputation: 464
When you present any model at that time form this model to not working pop view controller because this is presented model
for that you can use presented model parent Object Just like below
let presentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PresentVC") as! PresentVC
presentViewController.parentModel = self
self.presentViewController(presentViewController, animated: true, completion: nil)
and dismiss view controller like this
self.dismissViewControllerAnimated(true) {
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ViewControllers) {
parentModel.navigationController!.popToViewController(controller, animated: true)
break
}
}
}
and in block pop to your controller
Upvotes: 0
Reputation: 2172
You could do like below.
// First, finding specific Navigation Controller and do pop.
if let tabBarController = presentingViewController as? UITabBarController {
if let navigationController = tabBarController.viewControllers?[0] as? UINavigationController {
navigationController.popToRootViewController(animated: false)
// Then, dismiss self.
self.dismiss(animated: true, completion: nil)
}
}
Upvotes: 0
Reputation: 4375
You have to dismiss the ViewController C
in the following way.
self.presentingViewController
will give the previous view controller object.
Move to Root view controller
let presentingVC = self.presentingViewController
self.dismiss(animated: true) {
presentingVC?.navigationController?.popToRootViewController(animated: false)
}
Move to previous controller
If you need to previous view controller instead of root view controller then you have to just to a popViewController
let presentingVC = self.presentingViewController
self.dismiss(animated: true) {
presentingVC?.navigationController?.popViewController(animated: false)
}
Move to a specific View Controller
let presentingVC = self.presentingViewController
self.dismiss(animated: true) {
if let destinationVC = presentingVC?.navigationController?.viewControllers.filter({$0 is <Your Destination Class>}).first {
presentingVC?.navigationController?.popToViewController(destinationVC, animated: false)
}
}
<Your Destination Class>
replace with your destination class name.
Upvotes: 3
Reputation: 478
TRY BELOW CODE
self.navigationController?.dismiss(animated: true, completion: {
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: BVC) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
})
Upvotes: 0
Reputation: 931
In ViewController B, you can write below code.
self.navigationController?.dismiss(animated: true, completion: {
self.navigationController?.popToRootViewController(animated: true)
})
Upvotes: 0