gpt4 book ai didi

ios - 从特定的 childViewControllers 类调用方法

转载 作者:行者123 更新时间:2023-11-28 08:08:37 24 4
gpt4 key购买 nike

好吧,希望我能解释得尽可能清楚。

我有一个 Parent ViewController,它有一个 ContainerView,它将包含不同的 viewControllers/Class。我正在像这样在运行时更改此容器的内容:

    let newController = (storyboard?.instantiateViewController(withIdentifier: classIdentifierFromStoryboard))! as UIViewController
let oldController = childViewControllers.last! as UIViewController

oldController.willMove(toParentViewController: nil)
addChildViewController(newController)
newController.view.frame = oldController.view.frame

transition(from: oldController, to: newController, duration: 0, options: .transitionCrossDissolve, animations:{ () -> Void in
}, completion: { (finished) -> Void in

})

oldController.removeFromParentViewController()
newController.didMove(toParentViewController: self)

例如,在 UI 上显示的当前 Controller 是 firstVC,有点像

class firstVC {
func removeAnnotations() {
//something that removes annotation from MapVC
}
}

在父 Controller 上的某个事件上,我如何访问 firstVC 的实例以便我能够调用 removeAnnotations?截至目前我得到零,在父 Controller firstVC.removeAnnotations 上使用它,大概这调用了该类的另一个实例。任何帮助都会很棒!希望我的解释是有道理的。谢谢!

最佳答案

你可以像这样运行 subview Controller :

for case let vc as firstVC in self.childViewControllers {
vc.removeAnnotations()
}

这将运行所有类型为 firstVC 的 subview Controller 并执行 removeAnnotations 方法

关于ios - 从特定的 childViewControllers 类调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44382912/

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