zoukankan      html  css  js  c++  java
  • iOS父子控制器详解

    #import "ViewController.h"
    #import "ScoietyViewController.h"
    #import "HotViewController.h"
    #import "TopLineViewController.h"
    
    /*
        父子控制器:多控制器管理,导航控制器,UITabBarController
     
        默认UITabBarController,实现这种效果,父子实战
        永远只会显示一个view,把之前的view移除
        UITabBarController有个专门存放子控制器view,占位视图思想,1.不用去考虑子控制器的view尺寸 2.屏幕适配也不用管理
     
        1.添加所有子控制器
        2.设置对应按钮的内容,按钮内容由对应子控制器
     
     */
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *titleContainView;
    @property (weak, nonatomic) IBOutlet UIView *containView;
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 1.添加所有的子控制器
        [self setupAllViewController];
        // 2.设置按钮的内容
        [self setupTitleButton];
    }
    
    // 设置按钮的内容
    - (void)setupTitleButton
    {
        NSInteger count = self.titleContainView.subviews.count;
        for (int i = 0; i < count; i++) {
            UIButton *btn = self.titleContainView.subviews[i];
            UIViewController *vc = self.childViewControllers[i];
            [btn setTitle:vc.title forState:UIControlStateNormal];
        }
    }
    
    // 添加所有的子控制器
    - (void)setupAllViewController
    {
        // 社会
        ScoietyViewController *scoietyVc = [[ScoietyViewController alloc] init];
        scoietyVc.title = @"社会";
        [self addChildViewController:scoietyVc];
        
        // 头条
        TopLineViewController *topLineVc = [[TopLineViewController alloc] init];
        topLineVc.title = @"头条";
        [self addChildViewController:topLineVc];
        
        // 热点
        HotViewController *hotVc = [[HotViewController alloc] init];
        hotVc.title = @"热点";
        [self addChildViewController:hotVc];
        
    }
    
    // 点击标题按钮
    - (IBAction)showChildVcView:(UIButton *)sender {
        
        // 移除之前控制器的view
        [self.containView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //    for (UIView *vcView in self.containView.subviews) {
    //        [vcView removeFromSuperview];
    //    }
        
        // 把对应子控制器的view添加上去
        UIViewController *vc = self.childViewControllers[sender.tag];
        vc.view.backgroundColor = sender.backgroundColor;
        vc.view.frame = self.containView.bounds;
        [self.containView addSubview:vc.view];
    }
    
    @end
  • 相关阅读:
    爬取英雄联盟所有英雄皮肤
    Python xlrd模块读取Excel表中的数据
    H5的接口测试方式
    接口自动化
    DbUtils入门之QueryRunner
    常用注解
    修改Git下Git Bash开始键的默认起始路径
    SVN
    IDEA 2018 安装激活破解方法
    JVM原理(Java代码编译和执行的整个过程+JVM内存管理及垃圾回收机制)
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/10012939.html
Copyright © 2011-2022 走看看