我有一个奇怪的问题,我的应用程序中有一个滑动菜单,出于某种未知原因,每次我使用 .pushViewController 指令从一个 View 转到另一个 View 时,导航 Controller 的行为很有趣,它会重置我的 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.
但这显然没有发生,该按钮位于界面生成器中,当我在不使用幻灯片菜单的情况下进入该 View 时它会起作用,但当我单击我的幻灯片菜单的任何选项时它会消失。
这是我在该 View 的 viewDidLoad 方法上的代码
override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
Carrito.numProd = productosCarrito.count
print(productosCarrito.count)
var numeroProductos = String(Carrito.numProd)
navigationItem.rightBarButtonItem?.badgeValue = numeroProductos
}
如果我在不使用幻灯片菜单的情况下访问该页面(就像您在成功清除登录 View 后在那里进行切换时),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
}
编辑 MikeMTOL 的库有很多问题并且会导致很多问题,而不仅仅是这个问题,所以对于 Swift 用户我推荐这些扩展。 -> link
关于swift - UINavigationController 在 pushViewController UPDATE 2 之后表现得很有趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41514827/