IOS开发—通过ChildViewController实现view的切换

ChildViewController的应用

viewControlle中可以添加多个subView,在需要的时候显示出来;另一种方法是通过向parentViewController中可以添加多个childCiewController;来控制页面中的subView,降低代码耦合度;通过切换子视图控制器,可以显示不同的view;,替代之前的addSubView的管理。

本节通过类似百度新闻模块切换的界面来演示ChileViewController的应用:

文档结构:

代码演示:

#import "MainViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) FirstViewController *firstVC;
@property (nonatomic, strong) SecondViewController *secondVC;
@property (nonatomic, strong) ThirdViewController *thirdVC;
@property (nonatomic, strong) UIViewController *currentVC;

@property (nonatomic, strong) UIScrollView *headScrollView;
@property (nonatomic, strong) NSMutableArray *itemArray;
@property (nonatomic, strong) UIView *contentView;
@end

@implementation MainViewController
- (void)loadView{
    [super loadView];
    [self initialization];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadBaseUI];
}

- (void)initialization{
    _itemArray = [NSMutableArray arrayWithObjects:@"头条",@"今日",@"焦点", nil];
}

- (void)loadBaseUI{
    self.title = @"首页";
    _headScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    _headScrollView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    for (int i = 0; i<_itemArray.count; i++) {
        UIButton *itemButton = [[UIButton alloc]initWithFrame:CGRectMake(i*([UIScreen mainScreen].bounds.size.width/_itemArray.count), 0, [UIScreen mainScreen].bounds.size.width/_itemArray.count, 44)];
        itemButton.tag = 100+i;
        itemButton.backgroundColor = [UIColor clearColor];
        NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor purpleColor],NSFontAttributeName:[UIFont systemFontOfSize:14.0f]};
        [itemButton setAttributedTitle:[[NSAttributedString alloc]initWithString:_itemArray[i] attributes:dic] forState:UIControlStateNormal];
        [itemButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headScrollView addSubview:itemButton];
    }
    [_headScrollView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 44)];
    _headScrollView.showsHorizontalScrollIndicator = NO;
    _headScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_headScrollView];

    _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 44 - 64)];
    _contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_contentView];

    [self addSubControllers];
}

#pragma mark - privatemethods
- (void)addSubControllers{
    _firstVC = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    [self addChildViewController:_firstVC];

    _secondVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    [self addChildViewController:_secondVC];

    _thirdVC = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
    [self addChildViewController:_thirdVC];

    //调整子视图控制器的Frame已适应容器View
    [self fitFrameForChildViewController:_firstVC];
    //设置默认显示在容器View的内容
    [self.contentView addSubview:_firstVC.view];

    NSLog(@"%@",NSStringFromCGRect(self.contentView.frame));
    NSLog(@"%@",NSStringFromCGRect(_firstVC.view.frame));

    _currentVC = _firstVC;
}

- (void)buttonClick:(UIButton *)sender{
    if ((sender.tag == 100 && _currentVC == _firstVC) || (sender.tag == 101 && _currentVC == _secondVC) || (sender.tag == 102 && _currentVC == _thirdVC)) {
        return;
    }
    switch (sender.tag) {
        case 100:{
            [self fitFrameForChildViewController:_firstVC];
            [self transitionFromOldViewController:_currentVC toNewViewController:_firstVC];
        }
            break;
        case 101:{
            [self fitFrameForChildViewController:_secondVC];
            [self transitionFromOldViewController:_currentVC toNewViewController:_secondVC];
        }
            break;
        case 102:{
            [self fitFrameForChildViewController:_thirdVC];
            [self transitionFromOldViewController:_currentVC toNewViewController:_thirdVC];
        }
            break;
    }
}

- (void)fitFrameForChildViewController:(UIViewController *)chileViewController{
    CGRect frame = self.contentView.frame;
    frame.origin.y = 0;
    chileViewController.view.frame = frame;
}

//转换子视图控制器
- (void)transitionFromOldViewController:(UIViewController *)oldViewControllertoNewViewController:(UIViewController *)newViewController{
    [self transitionFromViewController:oldViewController toViewController:newViewController duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newViewController didMoveToParentViewController:self];
            _currentVC = newViewController;
        }else{
            _currentVC = oldViewController;
        }
    }];
}

//移除所有子视图控制器
- (void)removeAllChildViewControllers{
    for (UIViewController *vc in self.childViewControllers) {
        [vc willMoveToParentViewController:nil];
        [vc removeFromParentViewController];
    }
}

/**
 *  方法说明:
 *  1、addChildViewController:向父VC中添加子VC,添加之后自动调用willMoveToParentViewController:父VC
 *  2、removeFromParentViewController:将子VC从父VC中移除,移除之后自动调用
    didMoveToParentViewController:nil
 *  3、willMoveToParentViewController:  当向父VC添加子VC之后,该方法会自动调用。若要从父VC移除子VC,需要在移除之前调用该方法,传入参数nil。
 *  4、didMoveToParentViewController:  当向父VC添加子VC之后,该方法不会被自动调用,需要显示调用告诉编译器已经完成添加(事实上不调用该方法也不会有问题,不太明白); 从父VC移除子VC之后,该方法会自动调用,传入的参数为nil,所以不需要显示调用。
 */

/**
 *  注意点:
    要想切换子视图控制器a/b,a/b必须均已添加到父视图控制器中,不然会报错
 */
@end

最终效果:(实现了3个视图之间的切换)

  

时间: 2024-08-28 11:23:50

IOS开发—通过ChildViewController实现view的切换的相关文章

全面理解iOS开发中的Scroll View

转自:http://mobile.51cto.com/hot-430409.htm 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方法,但这些方法只是UIView一些属性的表面而已.因此,要想弄懂UIScrollView是怎么工作之前,你需要了解 UIView,特别是视图渲染过程的两步. 光栅化和组合 渲染过程的第一部分是众所周知的光栅化,光栅化简单的说就是产生一组绘图指令并且生成一张图片.比如绘制一个圆角矩形.带图片.标题居中的U

iOS开发之多表视图滑动切换示例(仿&quot;头条&quot;客户端)

好长时间没为大家带来iOS开发干货的东西了,今天给大家分享一个头条新闻客户端各个类别进行切换的一个示例.在Demo中对所需的组件进行的简单封装,在封装的组件中使用的是纯代码的形式,如果想要在项目中进行使用,稍微进行修改即可. 废话少说,先介绍一下功能点,下图是整个Demo的功能点,最上面左边的TabBarButtonItem是用来减少条目的,比如下图有三个按钮,点击减号会减少一个条目.右边的为增加一个条目.点击相应的按钮是切换到对应的表视图上,下方红色的是滑动的指示器,同时支持手势滑动.运行具体

iOS开发-ViewController的生命周期和切换

ViewController在App开发中是至关重要的一环,无论是页面的展示和数据之间的交互,ViewController提供了一个框架可以管理和构建App应用.iOS中构建App提供了两种方式一种是ViewController管理比如NavigationController和UITabBarController,另外一种就是我们经常打交道用到的自定义ViewController或者是常用UITableViewController展示我们需要的数据.下面这种苹果官网的图片可以加强理解: 生命周期

iOS开发-多语言本地化和手动切换

App要国际推广,需要支持多语言.可以通过本地化语言和切换来实现.本工程可以不切换系统语言,不重启应用直接切换自定义语言.现在只写中英文,其他语言也是同样处理方法.如果支持语言多,切换时间也不会花长时间,瞬间成功切换. 1.创建工程项目 2.配置项目本地化支持简体中文和英文       工程-PROJECT-info->Localizations,点"+",选择(Chinese(Simplified))添加简体中文,英文Xcode自带有(English),所以不需要再次添加. 3

【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

原文: http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(具体解释都写在了Demo里面的注释) [objc] view plaincopy // //  HMTMainViewController.m //  UIScrollView // //  Created

iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

本来仅仅是打算介绍一下addChildViewController这种方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(详细解释都写在了Demo里面的凝视) // // HMTMainViewController.m // UIScrollView // // Created by HMT on 14-6-25. // Copyright (c) 2014年 humingtao. All rights reserved. // #import

全面理解iOS开发中的Scroll View[转]

from:http://mobile.51cto.com/hot-430409.htm 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方法,但这些方法只是UIView一些属性的表面而已.因此,要想弄懂UIScrollView是怎么工作之前,你需要了解 UIView,特别是视图渲染过程的两步. AD: 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方法,但这些方法只是UI

iOS开发-进阶:语言国际化及代码切换语言

语言国际化和使用代码切换语言是很多APP都有的功能, 实现起来也很简单, 没有什么技术难题, 网上有很多相关的资料, 在实现了功能后, 打算把整个过程写在这里.(例子均为中英文, 替换其他语言原理相同, 只需记录保存该语言的key值即可) 需求: 1.应用名跟随系统切换 2.应用内容跟随系统切换 3.使用代码切换应用内容的语言(含后台返回数据切换语言) 实现: 1.应用名跟随系统切换 首先在Project下选择Info, 然后在Localizations中添加程序需要使用的语言,中文. 然后创建

iOS开发UI之自定义View

1. 通过代码自定义View,创建一个View类(继承UIView),一个View中包含一个UIImageView和一个UILabel 外界用alloc] init]方法创建对象时,系统默认会自动调用initWithFrame:(CGRect)frame方法,所以要创建对象View中的子控件需要重写initWithFrame:(CGRect)frame方法,在这个方法中来创建子控件,但不能对其根据创建这个类创建出来的对象尺寸来设置,需要调用 - (void)layoutSubviews 这个方法