zoukankan      html  css  js  c++  java
  • ParentViewController中添加SubViewController(IOS学习)

    我是用的是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"。

  • 相关阅读:
    python初级 0 出发吧
    10 个免费的服务器监控工具推荐
    Nginx 的线程池与性能剖析
    Java中 Comparator接口 与Comparable 的区别
    ORACLE分区表、分区索引详解
    搜索引擎爬虫蜘蛛的USERAGENT大全
    ios和android的发展前景比较
    DES、3DES、AES加密方式
    jsp、freemarker、velocity区别详解
    面向对象五大原则(SRP、OCP、LSP、DIP、ISP)
  • 原文地址:https://www.cnblogs.com/timelyxyz/p/3585055.html
Copyright © 2011-2022 走看看