【问题标题】:UINavigationController acting funny after pushViewController UPDATE 2在 pushViewController UPDATE 2 之后 UINavigationController 表现得很有趣
【发布时间】:2017-01-06 21:41:03
【问题描述】:

我遇到了一个奇怪的问题,我的应用程序中有一个幻灯片菜单,由于某种未知的原因,每次我使用 .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

一个 UINavigationItem 对象管理按钮和视图 显示在 UINavigationBar 对象中。构建导航时 接口,每个推入导航堆栈的视图控制器必须 有一个 UINavigationItem 对象,其中包含按钮并查看它 想要显示在导航栏中。管理的 UINavigationController 对象使用最顶层的导航项 两个视图控制器来填充导航栏的内容。

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

这是我对该视图的 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"

徽章值在消失前变成了那个数字,我也在用这个指令

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

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

Badge Value: Optional("40")

但不知什么原因消失了

UDATE 2

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

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

【问题讨论】:

  • 检查您是否没有在代码中手动设置rightBarButtonItemtintColor
  • 我删除了按钮的segue,它变成了蓝色,所以我改变了tint,现在tintColor没问题,但是badgevalue不知什么原因消失了。我检查并重新检查了代码,但没有手动设置。只有一个对按钮的引用。 @IBOutlet weak var carritoButton: UIBarButtonItem! 我尝试使用该引用来设置值,carritoButton.badgeValue = numeroProductos 在我着陆时它可以工作,但不是在与以前基本相同的幻灯片菜单上。
  • @Sulthan 我发现如果我关闭动画,徽章值不会消失,但我需要为幻灯片菜单设置动画。

标签: swift uibarbuttonitem uinavigationitem rightbarbuttonitem akswiftslidemenu


【解决方案1】:

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

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

编辑 MikeMTOL 的库有问题,不仅会导致很多问题,因此对于 Swift 用户,我推荐这些扩展。 -> link

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2012-02-19
    相关资源
    最近更新 更多