在rootViewController中
1 2
| [self.view addSubView:childViewController.view]; [self addChildViewController:childViewController];
|
如果view是在storyboard中设置的话,那么需要用
1 2
| UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; childViewController *cvc=[storyboard instantiateViewControllerWithIdentifier:@"cvc"];
|
如果用
1
| childViewController *cvc = [[childViewController alloc]init];
|
会导致subview一片空白。
至于在subview中向ParentView传递信息,可以使用delegate protocol
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| //childViewController.h @protocol DetailsViewControllerDelegate -(void)didClicked; @end @property (nonatomic,retain) id<DetailsViewControllerDelegate>_touchdelegate; //childViewController.m - (IBAction)buttomClicked:(id)sender { [self._touchdelegate didClicked]; } //ViewController.h @interface ViewController : UIViewController<DetailsViewControllerDelegate> // ViewController.m -(void)didClicked { self.text.text = @"123"; } - (IBAction)clicked:(id)sender { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; childViewController *cvc=[storyboard instantiateViewControllerWithIdentifier:@"cvc"]; CGRect frame = self.view.bounds; frame.origin.y = 110; cvc.view.frame = frame; [self addChildViewController:cvc]; [self.view addSubview:cvc.view]; cvc._touchdelegate = self; [cvc didMoveToParentViewController:self]; }
|
可以添加一个环境变量帮助查看错误信息