ios - 容器 View 中的数据仅传递给子 VC 一次

标签 ios swift uicontainerview

我正在尝试将数据传递到容器 View ,该 View 将根据用户状态包含多个 subview Controller 中的任何一个。我在第一次实例化新 VC 时成功传递了数据,但是如果我关闭并重新打开那个子 VC,数据就不会再次传递。这是按下按钮时的操作:

@IBAction func onMapButtonPressed(sender: UIButton) {
      let latitude = Double(selectedPlace.latitude!)
      let longitude = Double(selectedPlace.longitude!)
      bringInSubview("mapViewScreen")
      (childViewControllers[0] as! PlaceMapDetailVC).latitude = latitude!
      (childViewControllers[0] as! PlaceMapDetailVC).longitude = longitude!
      (childViewControllers[0] as! PlaceMapDetailVC).placeName = selectedPlace["name"] as! String
   }

以及辅助方法:

func bringInSubview(name:String) {

      newViewController = self.storyboard?.instantiateViewControllerWithIdentifier(name)
      newViewController!.view.translatesAutoresizingMaskIntoConstraints = false
      self.addChildViewController(self.newViewController!)
      self.addSubview(self.newViewController!.view, toView: self.containerView)
      view.bringSubviewToFront(containerView)
      view.bringSubviewToFront(closeSubviewButton)

      UIView.animateWithDuration(0.3, animations: {
         self.containerView.alpha = 1
         }, completion: { finished in
            // anything else needed in completion block?
      })
   }

func addSubview(subView:UIView, toView parentView:UIView) {
      parentView.addSubview(subView)
      var viewBindingsDict = [String: AnyObject]()
      viewBindingsDict["subView"] = subView
    parentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|",
         options: [], metrics: nil, views: viewBindingsDict))
      parentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|",
         options: [], metrics: nil, views: viewBindingsDict))
   }

最后摆脱了 child VC:

@IBAction func onCloseButtonInSubviewPressed(sender: UIButton) {
      UIView.animateWithDuration(0.3, animations: {
         self.containerView.alpha = 0
         self.view.sendSubviewToBack(self.closeSubviewButton)
         },
         completion: { finished in
            self.view.sendSubviewToBack(self.containerView)

            // clears out prior view that was there to free up memory
            for view in self.containerView.subviews {
               view.removeFromSuperview()
            }
      })
   }

非常感谢任何见解,谢谢!!!

最佳答案

我发现你的代码有问题。在 func onMapButtonPressed(sender: UIButton) 中,您假设位置始终为 0。它在第一次工作是因为它在那里。但是在您第一次删除它并在 bringInSubview(name:String) 中重新添加后,位置发生了变化。你应该检查一下。

希望对您有所帮助!

关于ios - 容器 View 中的数据仅传递给子 VC 一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620734/

相关文章:

ios - 在容器 View 中,导航 Controller 的导航栏未调整大小以包含状态栏

iphone - 为什么保留计数为负值?

ios - 谷歌地图方向

ios - 与 UITableViewDataSource 协议(protocol)混淆。与之前的冲突

swift - 为什么这个 JSON 解码会使我的应用程序崩溃?

ios - 显示/隐藏容器 View 时控制 View Controller 的生命周期

ios - 容器 View Controller 无法处理 Unwind Segue 操作

ios - “UIAlertView”在 iOS 9.0 中被弃用。将 UIAlertController 与 preferredStyle 的 UIAlertControllerStyleAlert 一起使用

swift - 初始化 PDFView 后无法访问 PDFDocument 属性

ios - Swift 4 将字符串解析为 json 对象