gpt4 book ai didi

swift - UINavigationcontroller : present and dismiss programmatically

转载 作者:行者123 更新时间:2023-11-30 12:00:47 24 4
gpt4 key购买 nike

我有一个 TableViewController,我想以模态方式呈现它,并且我需要它有一个导航栏。

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

一切正常,但我无法正确关闭该 Controller 。这是我到目前为止所得到的:

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

当我按下后退按钮时,没有任何变化,但导航栏变为空白(没有标题)。我有一种感觉,应用程序只是“忘记”了新导航 Controller 出现之前使用的导航 Controller 是什么。

我该如何解决这个问题?

最佳答案

尝试这样的事情:

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .done, target: self, action: #selector(dismissModal))
...
}

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

关于swift - UINavigationcontroller : present and dismiss programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47271959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com