如何同时关闭和弹出视图控制器
Posted
技术标签:
【中文标题】如何同时关闭和弹出视图控制器【英文标题】:How to dismiss and pop to viewcontroller simultaneously 【发布时间】:2017-11-23 23:31:08 【问题描述】:我的主视图控制器是 Tabbarcontroller
我从标签栏导航到 (A) Viewcontroller (TabarViewcontroller -> A(视图控制器) 从 A(视图控制器)我推送 (B) 视图控制器 从 B(视图控制器)我呈现 (C) 视图控制器 当我关闭 (c) 视图控制器时,我想显示 (A) 视图控制器 或(主页)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
【问题讨论】:
你知道 UNwindSegue 吗?? 【参考方案1】:您必须通过以下方式关闭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>
替换为您的目标类名称。
【讨论】:
它导航到根导航控制器(登录)我想将它导航到 A viewcontroller 或 Tabbarviewcontroller 更新了我的答案【参考方案2】:在 ViewController B 中,您可以编写以下代码。
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】:你可以像下面那样做。
// 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】:当您当时将该模型呈现为不工作的弹出视图控制器时,因为这是呈现模型
为此,您可以使用呈现的模型父对象,如下所示
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
并在块中弹出到您的控制器
【讨论】:
【参考方案6】:我必须使用 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)
也许这不是最好的答案,但它对我有用:)
【讨论】:
以上是关于如何同时关闭和弹出视图控制器的主要内容,如果未能解决你的问题,请参考以下文章