有没有办法将我的 `scrollView` 从我的主 class 连接或访问到另一个 class?

Is there a way to connect or access my `scrollView`from my main class to another class?

在我的 MainViewController 中,我有一个 scrollView 并在其中添加一个 ChildView,效果很好。在这个例子中,我实例化了我的 PlansViewController2 并将其作为 ChildView 添加到我的 scrollView.

选择 Cell 后,它会推送一个新控制器。但我想将新控制器作为 ChildView 添加到我的 `scrollView.


  @IBAction func planAction(_ sender: UIButton) {
    let newPlan = sender.frame.origin.x
    scrollView.subviews.forEach{[=12=].removeFromSuperview()}
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let nextController = storyBoard.instantiateViewController(withIdentifier: "PlansViewController2") as! PlansViewController2
    addChildView(viewController: nextController, in: scrollView)
    self.movingView.frame.origin.x = newPlan
}

我是我的 PlansViewController2 我推了另一个 ViewController 但我也想将它添加到我的 scrollView 来自以前的控制器。

let storyBoard = UIStoryboard(name:"Main", bundle:nil)
            let planViewCtrl = storyBoard.instantiateViewController(withIdentifier: "PlanViewCtrl") as! PlanViewController
            planViewCtrl.navigationItem.title = plan.title
            self.show(planViewCtrl, sender: self)
            _ = planViewCtrl.view // force view loading, so we can set it up properly
            _ = self.updateLastUsed(plan: plan)
            planViewCtrl.plan = plan

是否可以将 PlanViewController 从我的 MainViewController 添加到我的 scrollView

当您将视图的 PlansViewController2 添加到滚动视图时。请将 PlansViewController2 添加到 VC 的 childViewController 中,有 scrollView(self.addChild(nextController))。 当你想将 PlanViewController 添加到 scrollView 时:

  • 第 1 步:首先,您需要使用 parent 属性 (self.parent).
  • 获取具有 scrollView 的 viewcontroller
  • 第 2 步:最后,您可以访问 scrollView 实例,尽管 vc 您在第 1 步中获得。

祝你好运!