ios - 如何关闭所有以模态方式呈现在彼此面前的 View Controller ,并 swift 展开 segue?

标签 ios swift presentmodalviewcontroller unwind-segue dismissviewcontroller

所以我有一个主视图 Controller , View Controller 将在彼此的前面显示,我希望当用户单击最后一个 View Controller 中的按钮时关闭所有呈现的模态视图 Controller ,所以我使用了这段代码但我没有得到结果

let destination = matchViewController()
let appDelegate:UIApplicationDelegate = UIApplication.shared.delegate!
let initialViewController = destination
let navigationController = UINavigationController(rootViewController: initialViewController)
appDelegate.window??.rootViewController = navigationController
appDelegate.window??.makeKeyAndVisible()

我想用 unwind segue 退出 但是还有一个问题 最后一个 View Controller 将在许多不同的情况下出现多次,所以我只是关闭所有呈现的模态视图 Controller 。这种情况 我宁愿不使用 navigationController 但如果我必须使用它,请告诉我我应该在哪里使用它?

最佳答案

两种选择:

  1. 关闭 Root View Controller 上的所有 View Controller

    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
    
  2. 关闭所有 viewController,直到它有一个 presentingController

    func dismissAllControllers() {
    
        guard let vc = self.presentingViewController else { return }
    
        while (vc.presentingViewController != nil) {
            vc.dismiss(animated: true, completion: nil)
        }
    }
    

关于ios - 如何关闭所有以模态方式呈现在彼此面前的 View Controller ,并 swift 展开 segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53975698/

相关文章:

ios - 禁用 UIKeyboard 中的 "GO"按钮,直到满足特定条件

xcode - 试图将 UIView 移出屏幕并使用自动布局

ios - 如何以编程方式添加导航栏到presentModalViewController?

ios - 移动到另一个屏幕时停止呈现 View Controller

iOS (Swift) - UIViewController 数组

android - 调试需要服务器 URL 的应用程序

ios - 如何按数据添加顺序获取核心数据并按该顺序显示在 UITableView 中?

javascript - 动画和 Wow JS

ios - 如何观察图像旋转?

php - swift/MySQL/PHP 'Required Parameters Missing' 错误