如何解散导航控制器?

时间:2019-04-21 13:48:49

标签: swift uinavigationcontroller uisplitviewcontroller dismiss

我正在用showDetailViewController()呈现一个导航控制器(detailview)。我想在按下按钮时将其关闭。如何关闭该ViewController?

我尝试过的代码:

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

预期结果: 在按下按钮时关闭详细信息viewController。

实际结果: 不解雇。

2 个答案:

答案 0 :(得分:0)

好吧,一段时间以来,我一直在寻找一种处理此showDetailViewController的方法,尽管我不确定这是否能回答您的问题,但这是我的方法:

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

FirstController中,我实现了2个功能:一个显示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

尝试一下,希望对您有所帮助。

答案 1 :(得分:-1)

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

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