簡體   English   中英

在另一個UIViewController中顯示一個UIViewController

[英]Displaying a UIViewController within another UIViewController

我正在開發一個應用程序,其中UIViewControllerfirstViewController )包含一些UILabelsUIButtonUIViewsubView )。 UIView應該顯示包含一些圖層的UIViewControllersecondViewController )。 我無法做到這一點。

我應該怎么做才能firstViewController子視圖中顯示secondViewController?

您應該使用UIViewController包含或父/子視圖控制器。 您可以在此處閱讀詳細信息。

最基本的版本是:

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:消息(及其他消息)發送到子視圖控制器。

您可以通過將另一個視圖控制器的視圖添加為下面的子視圖來實現

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

您可以使用以下行添加它:

[self.subView addSubView:secondViewController.view];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM