将几个相互兄弟的VC添加到父VC

时间:2016-06-27 19:29:22

标签: ios objective-c

我正在制作一个带有地图视图控制器的iPad应用程序(如图所示)。

用户可以通过点击并按住地图位置(例如商家)来查看有关地图的详细信息,并显示DashboardViewController(橙色矩形)。

在仪表板中,用户可以选择查看其他相关数据集。 如果他们确实选择查看其他数据,则会显示ToolbarViewController(黄色矩形)和SpreadsheetViewController(绿色矩形)。工具栏用于管理数据集,允许用户使用上一个/下一个数据集填充电子表格,或添加/删除数据。

仪表板,工具栏和电子表格视图控制器之间相互之间进行了很多交流,因此它们应该被视为兄弟姐妹,而不是父母与子女彼此之间的关系。

即使这些部分遮挡了地图的某些部分,地图仍然可以通过平移/缩放手势进行触摸。

最后,更改地图的方向会导致3个部分(仪表板,工具栏,电子表格)调整大小/重新排列以适应新的方向。

此外,电子表格可由用户动态调整大小,因此他们可以用手指调整电子表格,使其扩展到屏幕底部。

零件的行为方式,所有展示时的不规则形状,以及地图需要保持平移/可缩放的事实,我需要将这些零件彼此分开,而不是为所有三个部分(仪表板,工具栏,电子表格)制作一个UIView的单个视图控制器,将它们添加为子视图,然后在地图上拍摄单个视图。

鉴于这种情况,我认为在每个VC片段上使用presentViewController完全没有意义,因为它是模态的,并且只允许三个片段中的一个一次可触摸。它还表明了各部分之间并不存在的等级关系。

我认为在这种情况下我需要做的是用addChildViewController将这些部分组合到地图的视图控制器中,以便它们彼此是兄弟姐妹,但是孩子们地图。

这是一种可行的方法,还是我误解了什么?

非常感谢。

enter image description here

1 个答案:

答案 0 :(得分:1)

是的,您可以将多个子视图控制器添加到单父视图控制器。对于您的解决方案,请根据需要制作视图。

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