2014-08-29 32 views
1

內顯示的UIViewController我開發一個應用程序,其中UIViewControllerfirstViewController)包含一些UILabels,一個UIButtonUIView子視圖)。 UIView應顯示包含一些圖層的UIViewControllersecondViewController)。我無法做到這一點。另一UIViewController中

我應該怎麼做,以顯示secondViewController子視圖firstViewController的?

+0

將第二個視圖控制器作爲childViewController添加到第一個viewController並將第二個視圖控制器的視圖添加爲子視圖。 – Akhilrajtr 2014-08-29 11:27:58

回答

4

你可以通過添加另外一個視圖控制器作爲視角子視圖波紋管

SecondVC *aObjSecondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"]; 
[self.view addSubview:aObjSecondVC.view] 
4

可以使用下面的行添加:

[self.subView addSubView:secondViewController.view]; 
+2

請注意,如果你這樣做,你將不會得到'viewWillAppear:'和'viewDidAppear:'事件。一個'UIViewController'是**不是**被設計爲像這樣添加。我強烈建議不要這種方法。看看MANIAK_dobrii的回答,這在我看來是正確的。 – 2014-08-29 13:14:01

+1

這爲什麼會有很多票?它不是正確的方式.. – tnylee 2014-08-29 16:58:19

6

您應該使用的UIViewController遏制或父/子視圖控制器。你可以閱讀詳細信息here

最基本的版本是:

UIViewController *parentVC = ... 
UIViewController *otherVC = ... // it's view will be added as subview 

[parentVC addChildViewController:otherVC]; 
[parentVC.containerView addSubview:otherVC.view]; // containerView is a view where your child view controller should go 
[otherVC didMoveToParentViewController:parentVC]; 

如果只添加其他視圖控制器的視圖作爲一個子視圖,子視圖控制器將不會接收所有事件。例如,如果您使用這裏建議的其他方法(只是將視圖添加爲子視圖,而沒有其他),則不會將-viewDidAppear:消息(和其他消息)發送到您的子視圖控制器。

相關問題