gpt4 book ai didi

iOS - 在父级插入 AnotherVC 后如何在 NavigationController 堆栈中找到 ChildVC

转载 作者:行者123 更新时间:2023-11-30 11:02:39 27 4
gpt4 key购买 nike

我有一个 tabBar,其中有一个选项卡,其中包含一个 NavVC,该 NavVC 的根作为 ParentVC。 ParentVC 有一个管理两个子VC(RedVC 和BlueVC)的segmentedControl。 RedVC 和 BlueVC 都包含一个按下 YellowVC 的按钮。

问题是当 YellowVC 被推送时,在它的 viewDidLoad 中我检查堆栈上的 View Controller ,出现的两个 Controller 是 ParentVC 和 YellowVC,没有提到 RedVC (如果它会插入它)或 BlueVC (如果它会插入它)。

for controller in navigationController!.viewControllers {

print(controller.description) // will only print ParentVC and YellowVC
}

我知道,由于 tabBar 有一个 navVC 作为选项卡,并且它的根是 ParentVC,那么这就是推送的根,但我需要知道当 YellowVC 被推送时,RedVC 或 BlueVC 中的哪一个触发了它。

我可以在 YellowVC 中使用一些类属性,但我想看看是否有通过 navigationController 的另一种方法:

var pushedFromRedVC = false // I'd prefer not to use these if I don't have to
var pushedFromBlueVC = false

当 RedVC 或 BlueVC 推送 YellowVC 时,我如何获得对 RedVC 或 BlueVC 的引用?

class MyTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

let parentVC = ParentVC()
let navVC = UINavigationController(rootViewController: parentVC)
navVC.tabBarItem = UITabBarItem(title: "Parent", image: nil), tag: 0)

viewControllers = [navVC]
}
}

class ParentVC: UIViewController {

var segmentedControl: UISegmentedControl! // switching segments will show either the redVC or the blueVC
let redVC = RedController()
let blueVC = BlueController()

override func viewDidLoad() {
super.viewDidLoad()

addChildViewController(redVC)
view.addSubview(redVC.view)
redVC.didMove(toParentViewController: self)

addChildViewController(blueVC)
view.addSubview(blueVC.view)
blueVC.didMove(toParentViewController: self)
}
}

class RedVC: UIViewController {

@IBAction func pushYellowVC(sender: UIButton) {

let yellowVC = YellowVC()
yellowVC.pushedFromRedVC = true // I'd rather not rely on this
navigationController?.pushViewController(yellowVC, animated: true)
}
}

class BlueVC: UIViewController {

@IBAction func pushYellowVC(sender: UIButton) {

let yellowVC = YellowVC()
yellowVC.pushedFromBlueVC = true // I'd rather not rely on this
navigationController?.pushViewController(yellowVC, animated: true)
}
}

class YellowVC: UIViewController {

var pushedFromRedVC = false // only sample, I'd rather not use these if I don't have to
var pushedFromBlueVC = false

override func viewDidLoad() {
super.viewDidLoad()


for controller in navigationController!.viewControllers {
// even though the push is triggered from either the RedVC or BlueVC it will only print ParentVC and YellowVC
print(controller.description)
}
}
}

最佳答案

由于 RedVCBlueVC 包含在 ParentVC 中,因此您不会在 UINavigationController 中找到对它们的任何引用> 堆栈。

使用两个 bool 值的替代方法是使用 sourceVC 属性:

class YellowVC: UIViewController {

var sourceVC: UIViewController?

override func viewDidLoad() {
super.viewDidLoad()

if self.sourceVC is BlueVC {
print("We came from Blue")
} else if self.sourceVC is RedVC {
print("We came from Red")
} else {
print("We came from somewhere else"
}
}
}


class RedVC: UIViewController {

@IBAction func pushYellowVC(sender: UIButton) {

let yellowVC = YellowVC()
yellowVC.sourceVC = self
navigationController?.pushViewController(yellowVC, animated: true)
}
}

class BlueVC: UIViewController {

@IBAction func pushYellowVC(sender: UIButton) {

let yellowVC = YellowVC()
yellowVC.sourceVC = self
navigationController?.pushViewController(yellowVC, animated: true)
}
}

您可能需要考虑 YellowVC 是否需要调用 Red/BlueVC 上的某些方法,或者只需要知道红/蓝源隐含的某些状态。

如果是后者,那么 RedVCBlueVC 可能应该使用委托(delegate)模式来让 ParentVC 知道它应该推送 YellowVC 并根据委托(delegate)调用的来源设置适当的状态属性。

您还可以考虑将状态放入模型中,然后传递给 VC,而不是 VC 本身的离散属性。

关于iOS - 在父级插入 AnotherVC 后如何在 NavigationController 堆栈中找到 ChildVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139276/

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