导航栏标题更新太晚(Swift)

Navigation bar title updates too late (Swift)

在显示某些类别的 collectionViewController 的一个单元格被 selected(都在 navigationViewController 内)之后,我有一个 tableViewController 显示。我想让导航栏显示 selected 类别的标题,但标题没有及时更新。

这意味着每当我返回 select 不同的类别时,它都会显示旧标题,即标题总是滞后于视图?

这是我在 collectionViewController 中的代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "CategorySelected") {
        let vc = segue.destination as! SearchOrgTableViewController
        vc.category = selectedCategory
    }
}

在 TableViewController 中:

var category: CategoryModel?{
    didSet{
        guard let name = category?.name else{
            return
        }
        self.navigationItem.title = name
    }
}

知道如何解决这个问题吗?

我试过直接在prepare函数中设置标题,结果一样,调用reloadInputViews()也没用。

试试这个代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "CategorySelected") {
DispatchQueue.main.async {
let vc = segue.destination as! SearchOrgTableViewController
    vc.category = selectedCategory
    vc.title = category.name } }
}

In iOS 10, UIKit has updated and unified background management for UINavigationBar, UITabBar, and UIToolbar. In particular, changes to background properties of these views (such as background or shadow images, or setting the bar style) may kick off a layout pass for the bar to resolve the new background appearance. In particular, this means that attempts to change the background appearance of these bars inside of -[UIView layoutSubviews], -[UIView updateConstraints], -[UIViewController willLayoutSubviews], -[UIViewController didLayoutSubviews], - [UIViewController updateViewConstraints], or any other method that is called in response to layout may result in a layout loop.

因此,这将 happens.try 更新视图中的标题是否加载或视图是否出现可能会对您有所帮助。