Reusing the view controller inside the scroll. The rapid development of iOS8

Using Swift for iOS8 in Xcode6, I need to create a scroll that will scroll dynamically generated maps. Maps contain their own unique images, text and buttons, but have the same layout. I am having trouble understanding how to create a reusable display controller in a storyboard from which I can build each map, and then embed them all in a scroll container so that I can move them one at a time.

I usually like to ask more specific questions about stack overflows, but after several hours of research I find it difficult; Your help will be greatly appreciated!

+7
ios xcode swift storyboard
source share
1 answer

You are looking for a custom container controller.

If this is programmatic, you call addChildViewController on the parent controller (thereby adding the child view controller to the view manager hierarchy), complete the entire configuration of the child view (including adding it to the parent controller view hierarchy), and then call didMoveToParentViewController on the child:

 let childController = self.storyboard?.instantiateViewControllerWithIdentifier("storyboardIdForChildScene") as UIViewController! addChildViewController(childController) childController.view.frame = ... scrollView.addSubview(childController.view) childController.didMoveToParentViewController(self) 

When you delete programmatically, you cancel this process by calling willMoveToParentViewController:nil for the child, remove the child view from your supervisor, and when done, call removeFromParentViewController :

 childController.willMoveToParentViewController(nil) childController.view.removeFromSuperview() childController.removeFromParentViewController() 

If you do this in Interface Builder, it’s much simpler, just drag and drop the “Container View” from the “Object Library” onto the scene of the parent view controller:

container view

For more information on how to do this, see Create Custom Container View Controllers in the View Controller Programming Guide. For a discussion of why it is important to perform these containment calls in order to maintain a view manager hierarchy synchronized with a view hierarchy, see WWDC 2011 Video Implementing the UIViewController Containment .

+11
source share

All Articles