【问题标题】:How to dismiss a navigation controller?如何关闭导航控制器?
【发布时间】:2019-04-21 13:48:48
【问题描述】:

我正在展示一个带有 showDetailViewController() 的导航控制器(detailview)。我想在按下按钮时将其关闭。如何关闭此视图控制器?

我尝试过的代码:

//detailviewcontroller
    @objc
    func cancel(_ sender: Any) {
        print("cancelPressed")
        //self.navigationController?.popViewController(animated: true)
        //self.navigationController?.dismiss(animated: true, completion: nil)
        //self.dismiss(animated: true, completion: nil)
        //splitViewController?.dismiss(animated: true, completion: nil)
        //splitViewController?.navigationController?.popToRootViewController(animated: true)
        //splitViewController?.navigationController?.popViewController(animated: true)
        //splitViewController?.navigationController?.dismiss(animated: true, completion: nil)
        //navigationController?.popViewController(animated: true)
        //navigationController?.dismiss(animated: true, completion: nil)
        //self.navigationController?.popToRootViewController(animated: true)
        //self.navigationController?.viewControllers.remove(at: 1)
        //self.navigationController?.viewControllers.remove(at: 0) - this one removes to blank view
        //self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

我已经尝试了多种 stackoverflow 解决方案:

Dismissing a navigation view controller

How to dismiss a view controller in a navigation controller, without dismissing the whole stack

ios swift - dismiss root view controller of navigation controller

Can't dismiss View Controller that's embedded in a Navigation Controller

How to dismiss a navigation controller presented from another navigation controller in iOS 10 and below?

Can't dismiss navigation controller in Swift

Can't dismiss navigation controller in Swift

Dismissing View Controller Doesn't Work While Using Navigation Controller

Dismiss current navigation controller when clicked tab bar

How to dismiss a certain view controller

我如何展示 detailviewcontroller:

//masterviewcontroller
// delegation for passing data between controllers
weak var passDelegate: PlaceSelectionDelegate?

func insertNewObject(_ sender: Any) {
    if let detailViewController = passDelegate as? DetailViewController {
        if let detailNavigationController = detailViewController.navigationController {
            detailViewController.delegate = self
            splitViewController?.showDetailViewController(detailNavigationController, sender: nil)
        }
    }
}

预期结果: 按下按钮时关闭详细视图控制器。

实际结果: 没有解雇。

【问题讨论】:

  • 关闭导航控制器后,拆分视图控制器的详细信息侧应该显示什么?

标签: swift uinavigationcontroller uisplitviewcontroller dismiss


【解决方案1】:

好的,我一直在寻找一种方法来处理这个showDetailViewController,虽然我不确定这是否能回答你的问题,但我就是这样做的:

我有两个控制器,我们称之为FirstControllerSecondController。它们都继承自UIViewController。当我单击FirstController 中的某个导航栏按钮时,我使用showDetailViewControllerSecondController 显示为向上移动的工作表,就像日历应用程序中的“日历”按钮一样。

FirstController 中,我实现了两个功能:一个显示SecondController,另一个关闭它。我在 SecondController 中调用了解除操作。它看起来像这样:

// FirstController
@objc func showSecondView() {
    let vc = SecondController()
    vc.delegate = self          
    let nav = UINavigationController(rootViewController:vc)            
    self.showDetailViewController(nav, sender: self)
}

func closeSecondView() {
    self.dismiss(animated: true)
}

如您所见,我将SecondController 包裹在UINavigationController 中,因此它显示了带有标题和所有内容的导航栏。代码是这样的:

// SecondController
weak var delegate: FirstController!

override func viewDidLoad() {
    super.viewDidLoad()
    title = "Second"
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(closeMe))
}

@objc func closeMe() {
    delegate.closeSecondView()
}

注意我是如何为FirstController 创建一个属性的,然后我用它来关闭SecondController。当我提出SecondController 时,我将self 指定为委托,这意味着我现在可以从其中调用closeSecondView 方法。

我不确定它是否适用于拆分视图。文档似乎建议无论何时使用它,如果屏幕足够大,它的行为就会有所不同,因为UISplitViewController 会覆盖它。它还说所有视图控制器都有这个方法。

看一看:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621432-showdetailviewcontroller

试一试,希望对你有帮助。

【讨论】:

    【解决方案2】:

    如果您只是想关闭导航控制器,这应该可以工作

    navigationController?.dismiss(animated: true, completion: nil)
    

    【讨论】:

    • 这在失败尝试列表中。事实上,它在列表中出现了两次。
    • 你的navigationController还没修好吗?
    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2019-05-04
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    相关资源
    最近更新 更多