在子视图中添加UIViewController [英] add UIViewController in subview

查看:109
本文介绍了在子视图中添加UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是搜索在子视图中添加UIViewController的正确键。
正如您在我的图像中看到的那样,有两个ViewController,主控制器和第二个控制器。在主控制器内部有一个UIView(蓝色背景色)。在UIView中,我想在我的UIView中添加第二个ViewController。我有这个代码,但它没有用。





这是我的代码

  #importViewController.h
#import SampleViewController.h
@interface ViewController()
@property(弱,非原子)IBOutlet UIView * testView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//加载视图后进行任何其他设置,通常是从笔尖。

SampleViewController * sample = [[SampleViewController alloc] initWithNibName:@SampleViewControllerbundle:nil];
sample.view.frame = CGRectMake(0,0,self.testView.bounds.size.width,self.testView.bounds.size.height);
[self.testView addSubview:sample.view];
}

@end

我想知道这是否有可能吗?我知道 initWithNibName:在xib文件中工作,我不是谷歌搜索关于此的确切术语。如果在IOS中这是可行的,我只是想尝试一些东西。希望你能理解我正在做的事情。希望得到你的建议。提前致谢



这是我的更新

  @interface ViewController()
@property(弱,非原子)IBOutlet UIView * testView;
@property(强,非原子)SampleViewController *样本;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//加载视图后进行任何其他设置,通常是从笔尖。

UIStoryboard * storyBoard = self.storyboard;
SampleViewController * sample = [storyBoard instantiateViewControllerWithIdentifier:@SampleViewController];
// SampleViewController * sample = [[SampleViewController alloc] // initWithNibName:@SampleViewControllerbundle:nil];

[self displayContentController:sample];
//注释了以下行,因为这里不需要它,当你想从父项中删除
//子视图时使用它。
// [self hideContentController:sample];

}

- (void)displayContentController:(UIViewController *)content;
{
[self addChildViewController:content]; // 1
content.view.bounds = self.testView.bounds; // 2
[self.testView addSubview:content.view];
[内容didMoveToParentViewController:self]; // 3
}


- (void)hideContentController:(UIViewController *)content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[内容removeFromParentViewController]; // 3
}

我总是收到此错误

 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在bundle中加载NIB:'NSBundle< / Users / ace / Library / Developer / CoreSimulator /Devices/035D6DD6-B6A5-4213-9FCA-ECE06ED837EC/data/Containers/Bundle/Application/EB07DD14-A6FF-4CF5-A369-45D6DBD7C0ED/Addsubviewcontroller.app> (加载)'名称'SampleViewController''

我认为,它正在寻找一个笔尖。我没有在这里实现一个笔尖。

解决方案

你应该使用Child Contament概念,这里MainViewController是一个父视图控制器和你想要将子视图控制器视图添加为主视图控制器上的子视图。



添加和删除儿童

  //调用displayContentController将SampleViewCOntroller视图添加到mainViewcontroller 
[self displayContentController:sampleVCObject];

//在MainViewController中编写此方法
- (void)displayContentController:(UIViewController *)content;
{
[self addChildViewController:content]; // 1
content.view.bounds = testView.bounds; // 2
[testView addSubview:content.view];
[内容didMoveToParentViewController:self]; // 3
}

以下是代码的作用:



它调用容器的addChildViewController:方法来添加子项。调用addChildViewController:方法也会自动调用子进程的willMoveToParentViewController:方法。
它访问子视图属性以检索视图并将其添加到自己的视图层次结构中。容器在添加视图之前设置子项的大小和位置;容器总是选择孩子的内容出现的位置。虽然此示例通过显式设置框架来执行此操作,但您也可以使用布局约束来确定视图的位置。
它显式调用子的didMoveToParentViewController:方法来表示操作已完成。

  //你也可以写MainViewController中的此方法用于删除之前添加的子VC。 
- (void)hideContentController:(UIViewController *)content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[内容removeFromParentViewController]; // 3
}

有关详细信息,请参阅apple doc:

当您使用一个或多个容器视图加载视图控制器时,Interface Builder还会加载与这些视图关联的子视图控制器。必须在父项的同时实例化子项,以便可以创建适当的父子关系。


I don't know if this is the right key to search "add UIViewController in subview". As what you can see in my image ,there are two ViewController, the main and the second controller. Inside the main controller there is a UIView(blue background color). Inside in UIView, I want to add the second ViewController in my UIView. I have this code but It didn't work.

here's my code

#import "ViewController.h"
#import "SampleViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *testView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    SampleViewController * sample = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
    sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);
    [self.testView addSubview:sample.view];
} 

@end

I want to know if this is possible? I know initWithNibName: works in xib file, I don't the exact term to search in google about this. I'm just trying to experiment something if this is possible in IOS. Hoping you understand what I'm trying to do. Hoping for your advice. Thanks in advance

here's my update

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *testView;
@property(strong,nonatomic) SampleViewController * samples;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

UIStoryboard *storyBoard = self.storyboard;
SampleViewController * sample = [storyBoard instantiateViewControllerWithIdentifier:@"SampleViewController"]; 
// SampleViewController * sample = [[SampleViewController alloc] //initWithNibName:@"SampleViewController" bundle:nil];

[self displayContentController:sample];
//commented the below line because it is not needed here, use it when you want to remove        
//child view from parent.
 //[self hideContentController:sample];

}

- (void) displayContentController: (UIViewController*) content;
{
    [self addChildViewController:content];                 // 1
    content.view.bounds = self.testView.bounds;                 //2
    [self.testView addSubview:content.view];
    [content didMoveToParentViewController:self];          // 3
}


- (void) hideContentController: (UIViewController*) content
{
    [content willMoveToParentViewController:nil];  // 1
    [content.view removeFromSuperview];            // 2
    [content removeFromParentViewController];      // 3
}

I always get this error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/ace/Library/Developer/CoreSimulator/Devices/035D6DD6-B6A5-4213-9FCA-ECE06ED837EC/data/Containers/Bundle/Application/EB07DD14-A6FF-4CF5-A369-45D6DBD7C0ED/Addsubviewcontroller.app> (loaded)' with name 'SampleViewController''

I think, its looking for a nib. I didn't implement a nib here.

解决方案

You should use Child containment concept, here MainViewController is a parent view controller and you want to add child view controller view as a subview on Main View Controller.

Adding and Removing a Child

//call displayContentController to add SampleViewCOntroller view to mainViewcontroller
 [self displayContentController:sampleVCObject];

// write this method in MainViewController
- (void) displayContentController: (UIViewController*) content;
{
   [self addChildViewController:content];                 // 1
   content.view.bounds = testView.bounds;                 //2
   [testView addSubview:content.view];
   [content didMoveToParentViewController:self];          // 3
}

Here’s what the code does:

It calls the container’s addChildViewController: method to add the child. Calling the addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically. It accesses the child’s view property to retrieve the view and adds it to its own view hierarchy. The container sets the child’s size and position before adding the view; containers always choose where the child’s content appears. Although this example does this by explicitly setting the frame, you could also use layout constraints to determine the view’s position. It explicitly calls the child’s didMoveToParentViewController: method to signal that the operation is complete.

//you can also write this method in MainViewController to remove the child VC you added before.
- (void) hideContentController: (UIViewController*) content
{
   [content willMoveToParentViewController:nil];  // 1
   [content.view removeFromSuperview];            // 2
   [content removeFromParentViewController];      // 3
}

For more details, please refer to apple doc: https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

Configuring a Container in Interface Builder, for those who don't want to write code.

To create a parent-child container relationship at design time, add a container view object to your storyboard scene, as shown in Figure 5-3. A container view object is a placeholder object that represents the contents of a child view controller. Use that view to size and position the child’s root view in relation to the other views in the container.

When you load a view controller with one or more container views, Interface Builder also loads the child view controllers associated with those views. The children must be instantiated at the same time as the parent so that the appropriate parent-child relationships can be created.

这篇关于在子视图中添加UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆