![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5SMyYDOzATM4QzNwcTM1EzLcRDM0EDMy8CXyQTN3AjNvwVavwVbvNmLn9GbiRXauNmLzV2Zh1Wavw1LcpDc0RHaiojIsJye.gif)
addchildviewcontroller
if the new child view controller is already the child of a container view controller, it is removed from that container before being added.
this method is only intended to be called by an implementation of a custom container view controller. if you override this method, you must call super in your implementation.
如果這個子 view controller 已經被添加到了一個容器 controller 當中,那在它被添加進新的容器controller之前會從舊的容器中移除.
這個方法隻能被用來實作一個自定義的容器controller添加子controller.如果你重寫了這個方法,你必須調用super方法.
使用源碼:
appdelegate.h + appdelegate.m
rootviewcontroller.h + rootviewcontroller.m
firstviewcontroller.h + firstviewcontroller.m
secondviewcontroller.h + secondviewcontroller.m
需要注意的地方:
1. 容器controller最好定義一個專門用來展示子controller相關view的區域,如例子中的,其中,maskstobounds很重要,要不然,整個controller都會被展示出來的.
self.showarea = [uiview new];
self.showarea.frame = cgrectmake(0, 0, 320, scr_height - downlenth - 10);
self.showarea.layer.maskstobounds = yes;
[self.view addsubview:_showarea];
[self.showarea addsubview:_firstvc.view];
2. 調用完addchildviewcontroller之後還需要調用didmovetoparentviewcontroller,官方文檔裡面有說明.
3. 為什麼在點選一個按鈕切換控制器的時候,showarea什麼都不用設定,為何還能顯示出變化呢?
其實這一點我也沒弄明白為何呢.
4. 這個與uitabbarcontroller的功能類似,都有懶加載功能,實際上可以用來當做模拟uitabbarcontroller使用,具備更高自由度的定制tabbar的功能.