在子视图中添加UIViewController

我不知道这是否是正确的关键来search“在子视图中添加UIViewController”。 正如你可以在我的图像中看到的,有两个ViewController,主和第二个控制器。 在主控制器里面有一个UIView(蓝色背景颜色)。 在UIView中,我想在我的UIView中添加第二个ViewController。 我有这个代码,但它没有工作。

在这里输入图像说明

这是我的代码

#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 

我想知道这是否可能? 我知道initWithNibName:在xib文件中工作,我没有确切的术语在谷歌search这个。 我只是想尝试一些东西,如果这可能在IOS中。 希望你明白我想要做什么。 希望得到您的build议。 提前致谢

这是我的更新

 @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 } 

我总是得到这个错误

 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'' 

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

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

添加和删​​除一个孩子

 //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 } 

代码如下:

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

 //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 } 

有关更多详细信息,请参阅apple文档: https : //developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

在Interface Builder中为不想编写代码的人configuration容器。

要在devise时创build父子容器关系,请将容器视图对象添加到故事板场景中,如图5-3所示。 容器视图对象是代表子视图控制器内容的占位符对象。 使用该视图来调整和定位与容器中其他视图相关的子视图。

在Interface Builder中添加容器视图 当您使用一个或多个容器视图加载视图控制器时,Interface Builder还会加载与这些视图关联的子视图控制器。 孩子必须与父母同时实例化,以便build立适当的亲子关系。

你可以简单地通过使用StoryBoards来做到这一点

  • 打开故事板,select你的蓝色视图所在的视图控制器,打开UtilitiessearchContainerView并拖动到你的蓝色视图,这将自动添加一个视图控制器,充当你的视图的子视图。 您可以在大小检查器中调整容器视图的大小。

在这里输入图像说明

我已经解决了关于第二个uiviewcontroller的uview的问题。 按照我使用的代码:

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; SecondViewController *sb = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:@"sb"]; sb.view.backgroundColor = [UIColor redColor]; [sb willMoveToParentViewController:self]; [self.view addSubview:sb.view]; [self addChildViewController:sb]; [sb didMoveToParentViewController:self]; 

我希望能帮助你。

非常感谢

你可以添加一个childViewController到UIViewController自iOS5以来..这是一个伟大的方式来制造更小,更可重用的viewControllers,我也非常喜欢它。

你真的很接近,但你只需要几行代码

 /// [self addChildViewController:sample]; [self.testView addSubview:sample.view]; //you already have this.. [sample didMoveToParentViewController:self]; 

在你viewWillDisappear:或其他拆解方法之一,你需要像这样清理:

 //we'll need another pointer to sample, make it an iVar / property.. [sample willMoveToParentViewController:nil]; // 1 [sample removeFromSuperview]; // 2 [sample removeFromParentViewController]; // 3 

你可以阅读苹果文档包含子ViewController在这里https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

我打算在子视图中添加UIViewController,这个工作。 在UIViewController我已经添加了2个button,在.h文件中:

 -(IBAction)matchButtonAction:(id)sender; -(IBAction)closeButtonAction:(id)sender; 

在.m文件中:

 -(IBAction)matchButtonAction:(id)sender { NSLog(@"matchButtonAction"); } -(IBAction)closeButtonAction:(id)sender { NSLog(@"closeButtonAction"); } 

但我没有看到日志。

我需要在UIViewController instantiateViewControllerWithIdentifier上添加相同的参数?

如何解决这个问题?

我的UIViewController初始化是:

 AlertDialogViewController *alertDialogView = (AlertDialogViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"alertDialogView"]; [alertDialogView willMoveToParentViewController:self]; [viewController.view addSubview:alertDialogView.view]; [viewController addChildViewController:alertDialogView]; [alertDialogView didMoveToParentViewController:viewController]; 
 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 addChildViewController:sample]; [self.testView addSubview:sample.view]; 

由于你的视图控制器是在故事板,你应该使用instantiateViewControllerWithIdentifier从故事板获取VC。

 SampleViewController * sample = [self.storyboard instantiateViewControllerWithIdentifier:@"IdOfSampleViewController"]; sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height); [self.testView addSubview:sample.view]; 

不要忘记在Storyboard中添加SampleViewController标识符

我试着使用这个代码:

SecondViewController * secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

  secondViewController.view.frame = CGRectMake(0, 0, self.mySubView.bounds.size.width, self.mySubView.bounds.size.height); [self addChildViewController:secondViewController]; [self.viewDialogSolution addSubview:secondViewController.view]; 

但我看到只有myview不是secondViewController的布局。

何解决疑难问题?

非常感谢