UINavigation控制器:以编程方式显示和解除

时间:2017-11-13 19:22:37

标签: swift uinavigationcontroller presentviewcontroller

我有一个TableViewController,我想以模态方式呈现,我需要它有一个NavigationBar。

要获得该导航栏,我有一个嵌入式UINavigationController,据我所知,UINavigationController是我必须以模态方式呈现的,所以这就是我所做的。

一切正常,但我无法正确解雇该控制器。这是我到目前为止所得到的:

func presentErrorMessages(errorMessages: [String]) {
    let storyBoard: UIStoryboard = UIStoryboard(name: "Message", bundle: nil)

    let infoMessagesNavigationViewController = storyBoard.instantiateViewController(withIdentifier: "InfoMessagesNavigation") as! ModalNavigationController

    let infoMessagesTableViewController = infoMessagesNavigationViewController.viewControllers[0] as! InfoMessagesTableViewController
    infoMessagesTableViewController.errorMessages = errorMessages

    self.navigationController?.present(infoMessagesNavigationViewController, animated: true)
}

我使用它来呈现ModalNavigationController,并将其解雇:

class ModalNavigationController: BaseNavigationController {

    var backNavItem = UINavigationItem()
    var okNavItem = UINavigationItem()

    override func viewDidLoad() {
        super.viewDidLoad()

        let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action:  #selector(dismissModal))
        backNavItem.leftBarButtonItem = backButton

        ...

        var items = [UINavigationItem]()
        items.append(backNavItem)

        self.navigationBar.items = items
    }

    @objc func dismissModal() {
        self.dismiss(animated: true)
    }
}

当我按下后退按钮时,没有任何变化,但导航栏变为空白(没有标题)。我觉得应用程序只是“忘记”在新的呈现之前使用的NavigationController。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

pip install -e /usr/local/lib/python2.7/dist-packages/pysolr.py

答案 1 :(得分:0)

我设法通过在我的TableViewController而不是我的NavigationController上放置和调用dismiss函数来解决问题:

...
public func setBackButton(){
    if self.navigationController != nil {
        let item = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(dismissModal))
        self.navigationItem.leftBarButtonItem = item
    }
}

@objc func dismissModal() {
    self.dismiss(animated: true)
}