tahoecoop
tahoecoop

Reputation: 378

Data is passed only once to child VC in container view

I am trying to pass data to a container view that will contain any one of several child view controllers depending on the user state. I am successfully passing data the first time the new VC is instantiated, but if I close and reopen that child VC, the data doesn't get passed through again. Here is the action when button is pressed:

@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
   }

And the helper methods:

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))
   }

And finally getting rid of the 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()
            }
      })
   }

Any insight is greatly appreciated, thank you!!!

Upvotes: 0

Views: 67

Answers (1)

vien vu
vien vu

Reputation: 4337

I see problem with your code. In func onMapButtonPressed(sender: UIButton) you assume that position is always is 0. It works at the first time because it is there. But after first time you remove it and re-add again in bringInSubview(name:String) the position is changed. You should check it.

Hope this help!

Upvotes: 1

Related Questions