Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss and pop to viewcontroller simultaneously

My home viewcontroller is Tabbarcontroller

  • From tabbar i navigate to (A) Viewcontroller (TabarViewcontroller -> A (Viewcontroller)
  • From A (Viewcontroller) i push (B) Viewcontroller
  • From B (Viewcontroller) i Present (C) Viewcontroller
  • When i dismiss (c) Viewcontroller i want to show (A) Viewcontroller or (Home) TabbarviewController

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

like image 526
user1374 Avatar asked Jun 21 '17 07:06

user1374


1 Answers

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.

like image 196
Subramanian P Avatar answered Nov 15 '22 10:11

Subramanian P