UIViewController 视图中的 iOS 嵌套视图控制器视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17499391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:15:24  来源:igfitidea点击:

iOS Nested View Controllers view inside UIViewController's view?

iosuiviewcontrollerviewcontroller

提问by Skyler

Is it typically bad programming practice in iOS to have a nested view controller's view inside UIViewController's view? Say for instance I wanted to have some kind of interactive element that responds to user's touches, but only takes up maybe 25% of the screen.

在 UIViewController 的视图中嵌套视图控制器的视图在 iOS 中通常是糟糕的编程实践吗?比如说,我想要某种交互元素来响应用户的触摸,但只占屏幕的 25%。

I suppose I would add this nested view controller to my UIViewController by saying something like:

我想我会通过这样说来将这个嵌套的视图控制器添加到我的 UIViewController 中:

[self.view addSubview: nestedViewController.view];

回答by Chris Wagner

No, this is generally good design, it helps keep your view controllers concise. However you should be using the view controller containment pattern, take a look at the following documentation.

不,这通常是好的设计,它有助于保持您的视图控制器简洁。但是,您应该使用视图控制器包含模式,请查看以下文档。

Implementing a Container View Controller

实现容器视图控制器

This is incredibly simple to setup using Interface Builder with Storyboards as well, take a look at the Container View in the object library.

使用带有 Storyboard 的 Interface Builder 进行设置也非常简单,请查看对象库中的容器视图。

Here is a contrived example in a Storyboard. In this example you would have 4 view controllers, one that holds the 3 containers, and one for each container. When you present the left most controller that has all of the containers, the Storyboard will automatically initialize and embed the other 3. You can access these child view controllers via the childViewControllersproperty or there is a method you can override prepareForSegue:sender:and capture the destination view controllers of the segue about to be called. This is also a good point to pass properties to the child view controllers if any are needed.

这是故事板中的一个人为示例。在本例中,您将有 4 个视图控制器,一个包含 3 个容器,一个用于每个容器。当您展示包含所有容器的最左侧控制器时,Storyboard 将自动初始化并嵌入其他 3 个。您可以通过childViewControllers属性访问这些子视图控制器,或者有一种方法可以覆盖prepareForSegue:sender:并捕获目标视图控制器即将被调用的 segue。如果需要,这也是将属性传递给子视图控制器的好方法。

enter image description here

在此处输入图片说明

回答by Maciej

I put this code in the parent view controller. It works great for me.

我将此代码放在父视图控制器中。这对我很有效。

Obj C

对象C

-(void)viewDidLoad{
     [super viewDidLoad];
     InnerViewController *innerViewController = [self.storyboard instantiateViewControllerWithIdentifier:INNER_VIEW_CONTROLLER];
     [self addChildViewController:innerViewController];
     [self.view addSubview:innerViewController.view];
     [innerViewController didMoveToParentViewController:self];
}

Swift:

斯威夫特

 let childViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController"),
 self.addChildViewController(childViewController)
 self.view.addSubview(childViewController.view)
 childViewController.didMove(toParentViewController: self)

Another option is to use IB and put container view. UIViewController will show up automatically (XCode 9 in this case): enter image description here

另一种选择是使用 IB 并放置容器视图。UIViewController 将自动显示(在本例中为 XCode 9): 在此处输入图片说明

回答by Rajat Jain

Here is my Swift 3 solution based on Swift Developers On FB's answer

这是我基于 Swift Developers On FB 回答的 Swift 3 解决方案

 let childViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildPageViewController"),
 self.addChildViewController(childViewController)
 self.view.addSubview(childViewController.view)
 childViewController.didMove(toParentViewController: self)