我是用的是Container.addSubView的方法。
1. ParentViewController.m的@interface()中添加2个子vc的实例变量,代码如下
1 @property (nonatomic, strong) EmojiViewController *emojiVC; 2 @property (nonatomic, strong) ICMsgTypeViewController *msgTypeVC;
2. 重写2个子vc的getter,因为复杂的页面切换逻辑很可能使2个子vc的实例被释放,所以在getter中作检查,并返回。代码如下
1 - (EmojiViewController *)emojiVC{ 2 if(!_emojiVC){ 3 _emojiVC = [self.storyboard instantiateViewControllerWithIdentifier:@"EmojiViewController"]; 4 } 5 return _emojiVC; 6 } 7 8 - (ICMsgTypeViewController *)msgTypeVC 9 { 10 if(!_msgTypeVC){ 11 _msgTypeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ICMsgTypeViewController"]; 12 } 13 return _msgTypeVC; 14 }
3. storyboard中添加2个子vc,并且设置Class和StoryboardID,用于获取实例
4. ParentViewController.m中添加代码加入子view(我没添加加入页面的相关animation和相对定位,so略丑'XDD)代码如下
1 - (IBAction)selectEmoji:(UIButton *)sender { 2 if (self.msgTypeVC.view.window) { 3 [self.msgTypeVC removeFromParentViewController]; 4 [self.msgTypeVC.view removeFromSuperview]; 5 } 6 [self addChildViewController:self.emojiVC]; 7 self.emojiVC.view.frame = CGRectMake(0, 300, 320, 100); 8 [self.view addSubview:self.emojiVC.view]; 9 [self.emojiVC didMoveToParentViewController:self]; 10 }
5. 总结,其实可以添加protocal优化这些代码,我是新手,待上手了再传demo,具体参考官方API"Creating Custom Container View Controllers"。