ios - 在另一个 UIViewController 中显示一个 UIViewController

标签 ios objective-c uiview uiviewcontroller subview

我正在开发一个应用程序,其中 UIViewController ( firstViewController ) 包含一些 UILabels , 一个 UIButton , 和 UIView ( subview )。 UIView应该显示 UIViewController ( secondViewController ) 包含一些层。我无法做到这一点。

我该怎么做才能显示 secondViewController 内 subview 第一 View Controller ?

最佳答案

您应该使用 UIViewController 包含或父/ subview Controller 。您可以阅读详情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];

如果只添加其他 View Controller 的 View 作为 subview , subview Controller 将不会收到所有事件。例如,如果您使用此处建议的其他方法(只需将 View 添加为 subview ,仅此而已),您将不会得到 -viewDidAppear:发送到您的 subview Controller 的消息(和其他)。

关于ios - 在另一个 UIViewController 中显示一个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25567297/

相关文章:

iOS:使用 CoreData 的聊天应用程序

iphone - 在后台围绕屏幕 float 一个 UIView

ios - 像 UIPopoverController 中那样关闭自定义 UIView

iphone - 强制横向应用程序遇到困难

ios - 'QBChatRoom' 已弃用 : Use QBChatDialog instead

ios - 发布没有“今天”扩展程序的应用程序?

ios - 如何从 NSURLSessionDataTask 和调用 block 中获取返回值?

objective-c - 如何在 header 中声明未知枚举?

ios - 我如何快速下载 GIF 图像

ios - "Failed to set plugin placeholders"消息?