gpt4 book ai didi

ios - (Swift) 弹回导航 Controller

转载 作者:行者123 更新时间:2023-12-01 18:35:23 35 4
gpt4 key购买 nike

我将描述我想做什么。我有 3 个 View Controller 。第一个应该是导航,我认为我在代码中犯了错误。
第一个 VC 通向第二个,第二个 VC 通向第三个 VC。第三个有一个按钮,应该返回第一个。

这就是我从 firstVC 中呈现 secondVC 的方式:

let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let secondVC = (mainStoryboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController)
presentVC(SecondVC)

func presentVC(_ VC: UIViewController) {
let navController = UINavigationController(rootViewController: VC)
navController.modalPresentationStyle = .fullScreen
self.navigationController?.present(navController, animated: true, completion: nil)
}

现在在第二个VC,当我点击 Close在右侧的 navBarItem 中,应该打开 thirdVC 并且它工作正常,这里是代码:

ViewDidLoad :
 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action:
#selector(closeSecondVC))

之后:
  @objc
func closeSecondVC() {
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let thirdVC = (mainStoryboard.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController)
presentVC(thirdVC) //Same function as above.
}

而在第三个VC中的这个buttonClick上,我需要返回到firstVC,这就是我迷失的地方:
 @IBAction func btnTapped(_ sender: Any) {
if let navController = self.navigationController {
navController.popViewController(animated: true)
} //nothing happens on click
}

最佳答案

在 btnTapped 中使用以下代码:

    for controller in self.navigationController!.viewControllers as Array 
{
// here YourViewController is your firstVC
if controller.isKind(of: YourViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}

并且不要展示你的观点,插入你的观点流行。

关于ios - (Swift) 弹回导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60187837/

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