gpt4 book ai didi

ios - 从导航堆栈中删除 View Controller 时如何删除导航栏项目?

转载 作者:行者123 更新时间:2023-11-28 11:32:19 26 4
gpt4 key购买 nike

一旦用户登录,我就会从导航堆栈中删除登录 View Controller 。但是,登录 View Controller 添加的导航栏按钮项目仍然存在。如何从导航堆栈中完全删除 View Controller ?

static func removePreviousFromNavigationStack(_ navVC: UINavigationController) {
var navArr = navVC.viewControllers
for elem in navArr {
if elem.isKind(of: LoginViewController.self) {
if let vc = StateData.loginVC {
vc.navigationItem.leftBarButtonItem = nil // is not working as intended
vc.navigationItem.rightBarButtonItem = nil
vc.navigationItem.title = "Foo"
}
//elem.removeFromParent()
}
}
navArr.remove(at: navArr.count - 2)
navVC.viewControllers = navArr
}

流程:HomeVC -> ApplyVC -> LoginVC -> FormVC

登录到 FormVC 后,我调用 remove 方法从堆栈中删除 LoginVC。这将删除 VC,但导航按钮仍然存在。如果我将导航按钮设置为 nil,则 ApplyVC 的 leftButtonItem、后退按钮、右键项目、主页按钮不会显示。有一个透明的后退按钮,单击该按钮后,会显示 ApplyVC 的导航栏按钮,就好像 LoginVC 从 View 中弹出一样,但不会对当前 View 进行任何更改.

screens

最佳答案

尝试在推送 FormVC 时删除 LoginVC 而不是在 FormVC 可见之后。

我创建了与您相同的 NavigationStack

1.HomeVC

中添加了一个 rightBarButton
class HomeVC: UIViewController {
var rightBarItem: UIBarButtonItem = {
return UIBarButtonItem(barButtonSystemItem: .bookmarks, target: nil, action: nil)
}()

override func viewDidLoad() {
super.viewDidLoad()
self.title = "HomeVC"
self.navigationItem.rightBarButtonItem = rightBarItem
}
}

class ApplyVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "ApplyVC"
}
}

2.LoginVC

中添加了一个 rightBarItem
class LoginVC: UIViewController {
var rightBarItem: UIBarButtonItem = {
return UIBarButtonItem(barButtonSystemItem: .add, target: nil, action: nil)
}()

override func viewDidLoad() {
super.viewDidLoad()
self.title = "LoginVC"
self.navigationItem.rightBarButtonItem = rightBarItem
}

@IBAction func onTapButton(_ sender: UIButton) {
var controllers = self.navigationController?.viewControllers
let formVC = self.storyboard?.instantiateViewController(withIdentifier: "FormVC") as! FormVC
controllers?.removeAll(where: { $0 is LoginVC })
controllers?.append(formVC)
if let controllers = controllers {
self.navigationController?.setViewControllers(controllers, animated: true)
}
}
}

在上面的代码中,我过滤了 LoginVC 并从 navigationController 的 viewControllers 数组 添加了 FormVC

class FormVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "FormVC"
}
}

enter image description here

关于ios - 从导航堆栈中删除 View Controller 时如何删除导航栏项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56424471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com