[英]How to add child ViewController's view to subview of the parent View controller?
[英]Add a child view controller's view to a subview of the parent view controller
我想添加一個tableViewController作為containerViewController的子視圖控制器(如下所示)。 根據Apple的View Controller Programming Guide,我可以通過我的containerViewController中的以下代碼行實現這一點:
[self addChildViewController:tableViewController];
[self.view addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
事實上,這很好。 現在的問題是我不想將tableViewController的視圖添加為containerViewController的根視圖的子視圖。 相反,我想將它添加為containerView的子視圖(參見圖像),它本身是containerViewController的根視圖的子視圖。 所以我改變了上面的代碼如下:
[self addChildViewController:tableViewController];
[self.contentView addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
現在,當我啟動應用程序時,它立即崩潰並出現此錯誤:
由於未捕獲的異常'UIViewControllerHierarchyInconsistency'而終止應用程序,原因:'子視圖控制器:應該有父視圖控制器:但實際的父級是:'
這里有什么問題? 是不是可以將childViewController的視圖添加到其containerViewController的特定子視圖中? 或者如何在視圖控制器層次結構中沒有錯誤的情況下實現此目的?
將viewController添加到哪個視圖並不重要。 如果將viewController的視圖添加到另一個viewController,則需要正確設置它。
tableViewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:tableViewController.view];
/*Calling the addChildViewController: method also calls
the child’s willMoveToParentViewController: method automatically */
[self addChildViewController:tableViewController];
[tableViewController didMoveToParentViewController:self];
//class name InfoViewController
IBOutlet UIView *addViewToAddPlot;
InfoViewController *InfoController;
-(void) add_method
{
InfoController = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
InfoController.view.frame = self.addViewToAddPlot.bounds;
[self containerAddChildViewController:InfoController];
}
-(void) remove_method
{
[self containerRemoveChildViewController : InfoController];
}
- (void)containerAddChildViewController:(UIViewController *)childViewController {
[self addChildViewController:childViewController];
[self.addViewToAddPlot addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];
}
- (void)containerRemoveChildViewController:(UIViewController *)childViewController {
[childViewController willMoveToParentViewController:nil];
[childViewController.view removeFromSuperview];
[childViewController removeFromParentViewController];
}
添加和刪除viewcontroller,#childviewcontroller
通過main_view_controller顯示child_view_controller。
第1步 :在故事板中創建main_view_controller。
第2步 :創建一個帶有UIview的child_view_controller,並在storyboard中創建一些Label 。
第3步 :在main_view_controller的按鈕操作中添加以下代碼:
- (IBAction)YourButtonAction:(id)sender {
ChildViewControllerName *childViewControllerName = [self.storyboard instantiateViewControllerWithIdentifier:@"storyboardIdYouProvided"];
childViewControllerName.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:childViewControllerName.view];
[childViewControllerName didMoveToParentViewController:self];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.