26

Tôi muốn thêm một tableViewController làm bộ điều khiển chế độ xem con của một containerViewController (được hiển thị bên dưới). Theo số View Controller Programming Guide của Apple, tôi có thể đạt được điều này bằng các dòng mã sau bên trong vùng chứa của tôiViewController:Thêm chế độ xem của trình điều khiển chế độ xem con vào chế độ xem phụ của bộ điều khiển chế độ xem cha mẹ

[self addChildViewController:tableViewController]; 
    [self.view addSubview:tableViewController.view]; 
    [tableViewController didMoveToParentViewController:self]; 

Thực tế, nó hoạt động tốt. Bây giờ vấn đề là tôi không muốn thêm xem tableViewController như là một subview của view gốc của containerViewController. Thay vào đó tôi muốn thêm nó như là một subview của containerView (xem hình ảnh) mà chính nó là một subview của view gốc của containerViewController. Vì vậy, tôi đã thay đổi mã trên như sau:

[self addChildViewController:tableViewController]; 
    [self.contentView addSubview:tableViewController.view]; 
    [tableViewController didMoveToParentViewController:self]; 

Bây giờ khi tôi khởi động ứng dụng nó bị treo ngay lập tức với lỗi này:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller: should have parent view controller: but actual parent is:'

vấn đề là gì đây? Không thể thêm chế độ xem của childViewController vào một chế độ xem riêng của vùng chứaViewController của nó? Hoặc làm thế nào tôi có thể đạt được điều này mà không có một lỗi trong hệ thống phân cấp bộ điều khiển xem?

containerViewController

Trả lời

48

Nó không quan trọng mà xem bạn đang thêm đứa trẻ viewController tới. Nếu một khung nhìn của một viewController được thêm vào một viewController khác, bạn cần thiết lập nó một cách đúng đắn.

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]; 

Source code

+0

Bạn có chắc chắn về điều đó và bạn đã thử nghiệm mã? Tôi đã thử điều đó và nó * làm * tạo sự khác biệt cho dù bạn thêm khung nhìn của childViewController vào khung nhìn gốc của containerViewController hay vào bất kỳ phần phụ nào của nó. – Mischa

+2

@Mischa Mã nguồn bao gồm câu trả lời. Hãy kiểm tra. – Anupdas

+4

+1 cho tableViewController.view.frame = self.contentView.bounds; – onmyway133

1
//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]; 

} 

Thêm và loại bỏ viewController, # childviewcontroller

1

Để hiển thị một child_view_controller hơn một main_view_controller.

bước 1: tạo main_view_controller trong bảng phân cảnh.

bước 2: tạo child_view_controller bằng UIview và một số Nhãn bên trong trong bảng phân cảnh.

bước 3: trong hành động nút main_view_controller của thêm đoạn mã sau:

- (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]; 
}