UINavigationController 在 pushViewController UPDATE 2 之后表现得很有趣

UINavigationController acting funny after pushViewController UPDATE 2

我有一个奇怪的问题,我的应用程序中有一个幻灯片菜单,出于某种未知原因,每次我使用 .pushViewController 指令从一个视图转到另一个视图时,导航控制器的行为很有趣,它会重置我的UIBarButtonItems。 (它们更改为原来的 tintcolor,badgeValue 消失)。

这是我在幻灯片菜单中用来进行转换的方法:

func openViewControllerBasedOnIdentifier(_ strIdentifier:String){
    let destViewController : UIViewController = self.storyboard!.instantiateViewController(withIdentifier: strIdentifier)


    let topViewController : UIViewController = self.navigationController!.topViewController!

    if (topViewController.restorationIdentifier! == destViewController.restorationIdentifier!){
        print("Same VC")
    } else {
        var numeroProductos = String(Carrito.numProd)

        self.navigationController!.pushViewController(destViewController, animated: true)


    }
}

func slideMenuItemSelectedAtIndex(_ index: Int32) {
    let topViewController: UIViewController = self.navigationController!.topViewController!
    print("View Controller is : \(topViewController) \n", terminator: "")
    switch(index) {
    case 0:
        print("Home\n", terminator: "")

        self.openViewControllerBasedOnIdentifier("Home")

        break

    case 1:
        print("Play\n", terminator: "")

        self.openViewControllerBasedOnIdentifier("MiCuenta")

        break

    case 2:
        print("Play\n", terminator: "")

        self.openViewControllerBasedOnIdentifier("QuienesSomos")

        break

    case 3:
        print("Play\n", terminator: "")

        self.openViewControllerBasedOnIdentifier("NuestraCausa")

        break

    case 4:
        print("Play\n", terminator: "")

        self.openViewControllerBasedOnIdentifier("Contacto")

        break

    case 5:
        print("Play\n", terminator: "")

        self.openViewControllerBasedOnIdentifier("FAQ")

        break

    default:
        print("default\n", terminator: "")
    }
}

根据Apple's own documentation

A UINavigationItem object manages the buttons and views to be displayed in a UINavigationBar object. When building a navigation interface, each view controller pushed onto the navigation stack must have a UINavigationItem object that contains the buttons and views it wants displayed in the navigation bar. The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content.

但这显然没有发生,该按钮位于界面生成器中,当我在不使用幻灯片菜单的情况下进入该视图时它会起作用,但当我单击我的幻灯片菜单的任何选项时它会消失。

这是我在该视图的 viewDidLoad 方法中的代码

override func viewDidLoad() {
    super.viewDidLoad()
    addSlideMenuButton()
    Carrito.numProd = productosCarrito.count
    print(productosCarrito.count)
    var numeroProductos = String(Carrito.numProd)
    navigationItem.rightBarButtonItem?.badgeValue = numeroProductos

}

如果我在不使用幻灯片菜单的情况下到达该页面(就像您在成功清除登录视图后在那里进行搜索时),badgeValue 会正确显示

但如果我使用幻灯片菜单,就会发生这种情况

关于可能导致此问题的原因有什么想法吗?

更新

我发现了一些东西。

如果我在 openVIewControllerBasedOnIdentifier 方法或 slideMenuSelectedAtIndex 中插入此指令

navigationItem.rightBarButtonItem?.badgeValue = "25"

badgevalue 在消失前更改为该数字,我也在使用此指令

print("Badge Value:\(navigationItem.rightBarButtonItem?.badgeValue as Any)")

所以值在那里,因为我在调试控制台中得到了这个:

Badge Value: Optional("40")

但由于某些未知原因它消失了

更新 2

 self.navigationController!.pushViewController(destViewController, animated: false)

我发现如果我关闭动画,徽章值不会消失,但我也需要动画才能工作。

我找到了解决方案,实际上很简单,为避免这种行为,应将 badgeValue 设置为 viewDidLayoutSubViews() 而不是 viewDidLoad()

override func viewDidLayoutSubviews() {
    var numeroProductos = String(Carrito.numProd)
    carritoButton.badgeValue = numeroProductos
}

EDIT MikeMTOL 的库有问题并且会导致很多问题,而不仅仅是这个问题所以对于 Swift 用户我推荐这些扩展。 -> link