我正在尝试将导航后退按钮标题更改为“返回”并尝试从 navigationController 中删除一系列 viewControllers 。我的代码块如下:
func SetUpNavigationBackButtonAndNavigationViewStack()
{
var viewControllers: [UIViewController] = self.navigationController?.viewControllers as [UIViewController]
if(self.navigationController?.viewControllers[viewControllers.count - 2].nibName == "OrderApprovalVC"
&& OrderBag.bag.count == 0)
{
var newBackButton: UIBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: nil, action: nil)
self.navigationItem.backBarButtonItem = newBackButton
let navigationArray: Array<UIViewController> = self.navigationController?.viewControllers as [UIViewController]
for(var i=1; i<navigationArray.count-1; i++)
{
viewControllers.removeAtIndex(1)
}
self.navigationController?.viewControllers = viewControllers
viewControllers.removeAll(keepCapacity: false)
}
}
我运行了这段代码,我看到了它,它从堆栈视图中删除了所需的 viewcontrollers ,然后由于点击后退按钮返回。但是,在返回操作后,应该不存在后退按钮,但它存在。此外,如果单击后退按钮,已删除的viewcontrollers 仍会显示但不会导航它们,但会删除这些viewcontrollers。 此外,我的后退按钮标题没有改变。我怎么解决这个问题 ?什么是最好的解决方案?
答案 0 :(得分:1)
您应该更好地使用UINavigationController
提供的方法,而不是从导航堆栈中删除viewControllers:
func popToViewController(_ viewController: UIViewController, animated animated: Bool) -> [AnyObject]?
或者,一直回到rootViewController:
func popToRootViewControllerAnimated(_ animated: Bool) -> [AnyObject]?
这会自动从导航堆栈中释放并删除viewControllers。