如何同时关闭并返回到指定视图控制器

7
我家的视图控制器是Tabbar控制器。
从Tabbar我导航到(A)视图控制器(TabarViewcontroller -> A(视图控制器))。
从A(视图控制器)推入(B)视图控制器。
从B(视图控制器)呈现(C)视图控制器。
当我解除(C)视图控制器时,我想显示(A)视图控制器或(Home)Tabbar视图控制器。
因此,我想先解除呈现的视图控制器,然后再弹出之前推送的控制器。
以下是我的导航流程。
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


你知道UNwindSegue是什么吗? - Matloob Hasnain
6个回答

3

您需要以以下方式解除 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>替换为您的目标类名。

它正在导航到根导航控制器(登录),我想将其导航到一个视图控制器或选项卡视图控制器。 - user1374
更新了我的回答。 - Subramanian P

0
在B视图控制器中,您可以编写以下代码。
self.navigationController?.dismiss(animated: true, completion: {
     self.navigationController?.popToRootViewController(animated: true)
})

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

})


0
你可以像下面这样做。
    // 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)
        }
    }

0

当您在展示任何模型时,不要从该模型转到不工作的弹出视图控制器,因为这是已呈现的模型

为此,您可以使用已呈现的模型父对象,就像下面这样

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

并在块中弹出到您的控制器


0

我不得不使用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)
}

也许这不是最好的答案,但对我来说有效 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接