簡體   English   中英

如何同時關閉和彈出視圖控制器

[英]How to dismiss and pop to viewcontroller simultaneously

我的主視圖控制器是 Tabbarcontroller

  • 從標簽欄我導航到 (A) Viewcontroller (TabarViewcontroller -> A (Viewcontroller)
  • 從 A(視圖控制器)我推(B)視圖控制器
  • 從 B (Viewcontroller) i Present (C) Viewcontroller
  • 當我關閉 (c) Viewcontroller 我想顯示 (A) Viewcontroller 或 (Home) TabbarviewController

所以,我想首先關閉呈現的視圖控制器,然后我想彈出我以前推送的控制器

這是我的導航流程

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)

所以從 C Viewcontroller 當我解散我想顯示 Tabbarviewcontroller 或 (A) ViewController

您必須通過以下方式關閉ViewController C

self.presentingViewController將給出之前的視圖控制器對象。

移動到根視圖控制器

  let presentingVC = self.presentingViewController
  self.dismiss(animated: true) { 
      presentingVC?.navigationController?.popToRootViewController(animated: false)
  }

移至上一個控制器

如果您需要以前的視圖控制器而不是根視圖控制器,那么您只需使用 popViewController

let presentingVC = self.presentingViewController
 self.dismiss(animated: true) {
      presentingVC?.navigationController?.popViewController(animated: false)
 }

移動到特定的視圖控制器

    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>替換為您的目標班級名稱。

在 ViewController B 中,您可以編寫以下代碼。

self.navigationController?.dismiss(animated: true, completion: {
     self.navigationController?.popToRootViewController(animated: true)
})
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
          }
    }

})

你可以像下面這樣做。

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

當您當時呈現任何模型時,將此模型形成為不工作的流行視圖控制器,因為這是呈現模型

為此,您可以使用呈現的模型父對象就像下面

let presentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PresentVC") as! PresentVC
presentViewController.parentModel = self
self.presentViewController(presentViewController, animated: true, completion: nil)

並像這樣關閉視圖控制器

self.dismissViewControllerAnimated(true) { 

           for controller in self.navigationController!.viewControllers as Array {
                if controller.isKind(of: ViewControllers) {
                     parentModel.navigationController!.popToViewController(controller, animated: true)
                     break
                }
           }
      }

並在塊彈出到您的控制器

我不得不使用 NotificationCenter 來彈出以前的控制器。

let back_view = Notification.Name("back_view")

//這里是關閉實際控制器的動作

 @IBAction func closeWW(_ sender: Any) {
                
        self.dismiss(animated: true, completion: {
            NotificationCenter.default.post(name: self.back_view, object: nil)
        })
    }

//現在,在之前的控制器中

override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(popView), name: back_view, object: nil)
}

@objc func popView(){
    self.navigationController?.popViewController(animated: true)
}

也許這不是最好的答案,但它對我有用:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM