How to dismiss and pop to viewcontroller simultaneously

别来无恙 提交于 2021-01-27 04:22:10

问题


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


回答1:


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.




回答2:


In ViewController B, you can write below code.

self.navigationController?.dismiss(animated: true, completion: {
     self.navigationController?.popToRootViewController(animated: true)
})



回答3:


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
          }
    }

})




回答4:


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)
        }
    }



回答5:


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




回答6:


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 :)



来源:https://stackoverflow.com/questions/44669698/how-to-dismiss-and-pop-to-viewcontroller-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!