Logicsaurus Rex Logicsaurus Rex - 5 months ago 9
iOS Question

Adding several VC's who are siblings of each other, to a parent VC

I am making an iPad app with a map view controller (shown).

The user can view details about the map by tap-and-hold on a map location (such as a business), and the

DashboardViewController
(orange rectangle) appears.

From the dashboard, the user can choose to see additional related datasets.
If they do choose to see additional data, then the
ToolbarViewController
(yellow rectangle) and
SpreadsheetViewController
(green rectangle) will appear. The toolbar is to manage the datasets, allowing the user to populate the spreadsheet with previous/next dataset, or to add/delete pieces of data.

The dashboard, toolbar, and spreadsheet view controllers communicate a lot between each other, so they should really be considered siblings more than parent-child in how they relate to each other.

The map remains touchable with pan/zoom gestures even while these parts are obscuring portions of the map.

Lastly, changing the orientation of the map causes the 3 parts (dashboard, toolbar, spreadsheet) to resize/rearrange themselves to fit the new orientation.

Also, the spreadsheet is dynamically sizable by the user, so they can resize it with a finger to make it extend to the bottom of the screen.

The way the parts behave, the irregular shape they make when all on display, and the fact that the map needs to remain pannable/zoomable, I require keeping these parts separate from each other, rather than make a single view controller with a
UIView
for all three parts (dashboard, toolbar, spreadsheet), add them as subviews, and slap that single view over the map.

Given this scenario, I'm assuming it makes no sense at all to use
presentViewController
on each VC piece as that is modal and would only allow one of the three pieces to be touchable at a time. It also suggests a hierarchical relationship that doesn't really exist between the parts.

I assume what I need to do in this scenario is assemble these pieces with
addChildViewController
adding each of them to the map's view controller, so that they are siblings with each other, but children of the map.


Is that a workable approach, or am I misunderstanding something?

Thanks a lot.

enter image description here

Answer

Yes you can add multiple child view controllers to single parent view controller. For your solution, make as much as view as you want.

// view controller that we want to add
let child1VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Child1VC")

// create view section where we will add the child view controller
let viewWhereIWillAddChild1VC = UIView(frame: CGRect(x:0, y:0, width: 200, height: 200))

// add view controller to parent view controller i.e. self
self.addChildViewController(child1VC)

// change frame of child view controller's view to be equal to frame of view where we will add it
child1VC.view.frame = viewWhereIWillAddChild1VC.frame

//adding view of child view controller to the designated view area
viewWhereIWillAddChild1VC.addSubview(child1VC.view)

//specifying is child is moved to parent view controller which is self
child1VC.didMoveToParentViewController(self)

//finally adding the designated view area in self where we kept child view controller's view
self.view.addSubview(viewWhereIWillAddChild1VC)

// now ADDING second view controller

let child2VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Child2VC")
let viewWhereIWillAddChild2VC = UIView(frame: CGRect(x:200, y:0, width: 200, height: 200))
addChildViewController(child2VC)
child2VC.view.frame = viewWhereIWillAddChild2VC.frame
viewWhereIWillAddChild2VC.addSubview(child2VC.view)
child2VC.didMoveToParentViewController(self)
self.view.addSubview(viewWhereIWillAddChild2VC)