使用addChildViewController手动控制UIViewController的切换
addChildViewController
If the new child view controller is already the child of a container view controller, it is removed from that container before being added.
This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must call super in your implementation.
如果这个子 view controller 已经被添加到了一个容器 controller 当中,那在它被添加进新的容器controller之前会从旧的容器中移除.
这个方法只能被用来实现一个自定义的容器controller添加子controller.如果你重写了这个方法,你必须调用super方法.
使用源码:
AppDelegate.h + AppDelegate.m
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "RootViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 加载根视图控制器
self.window.rootViewController = [RootViewController new]; self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} @end
RootViewController.h + RootViewController.m
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h" // 获取当前屏幕尺寸
#define SCR_HEIGHT [UIScreen mainScreen].bounds.size.height // 设置按钮高度
static CGFloat downLenth = .f; // 标示button的枚举值
typedef enum
{ BUTTON_1 = 0x11,
BUTTON_2, } EFlags; @interface RootViewController () { UIViewController *currentVC; } @property (nonatomic, strong) UIView *showArea; // 加载子controller的view
@property (nonatomic, strong) FirstViewController *firstVC; // 子controller
@property (nonatomic, strong) SecondViewController *secondVC; // 子controller @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化控制器
[self controllersInit]; // 初始化要展示的区域
[self showAreaInit]; // 初始化按钮
[self buttonsInit];
} #pragma mark - 初始化控制器
- (void)controllersInit
{
// 初始化两个控制器并作为root控制器的subController
_firstVC = [FirstViewController new];
[self addChildViewController:_firstVC];
[_firstVC didMoveToParentViewController:self]; _secondVC = [SecondViewController new];
[self addChildViewController:_secondVC];
[_secondVC didMoveToParentViewController:self];
} #pragma mark - 初始化要展示的区域
- (void)showAreaInit
{
// 初始化要展示的区域
self.showArea = [UIView new];
self.showArea.frame = CGRectMake(, , , SCR_HEIGHT - downLenth - );
self.showArea.layer.masksToBounds = YES;
[self.view addSubview:_showArea]; // 将第一个控制器的view添加进来展示
[self.showArea addSubview:_firstVC.view]; currentVC = _firstVC;
} #pragma mark - 初始化按钮以及按钮事件
- (void)buttonsInit
{
UIButton *firstVCButton = [UIButton new];
[self.view addSubview:firstVCButton];
firstVCButton.backgroundColor = [UIColor redColor];
firstVCButton.tag = BUTTON_1;
firstVCButton.frame = CGRectMake(, SCR_HEIGHT - downLenth, / , downLenth);
[firstVCButton addTarget:self
action:@selector(buttonsEvent:)
forControlEvents:UIControlEventTouchUpInside]; UIButton *secondVCButton = [UIButton new];
[self.view addSubview:secondVCButton];
secondVCButton.backgroundColor = [UIColor yellowColor];
secondVCButton.tag = BUTTON_2;
secondVCButton.frame = CGRectMake( / , SCR_HEIGHT - downLenth, / , downLenth);
[secondVCButton addTarget:self
action:@selector(buttonsEvent:)
forControlEvents:UIControlEventTouchUpInside];
} - (void)buttonsEvent:(UIButton *)button
{
if (button.tag == BUTTON_1)
{
if (currentVC == _firstVC)
{
return;
} [self transitionFromViewController:currentVC
toViewController:_firstVC
duration:
options:UIViewAnimationOptionTransitionNone
animations:^{ }
completion:^(BOOL finished) {
currentVC = _firstVC;
}];
} if (button.tag == BUTTON_2)
{
if (currentVC == _secondVC)
{
return;
} [self transitionFromViewController:currentVC
toViewController:_secondVC
duration:
options:UIViewAnimationOptionTransitionNone
animations:^{ }
completion:^(BOOL finished) {
currentVC = _secondVC;
}];
}
} @end
FirstViewController.h + FirstViewController.m
#import <UIKit/UIKit.h> @interface FristViewController : UIViewController @end
#import "FristViewController.h" @interface FristViewController () @end @implementation FristViewController - (void)viewDidLoad
{
[super viewDidLoad]; NSLog(@"FirstViewController viewDidLoad");
} - (void)viewWillAppear:(BOOL)animated
{
NSLog(@"FirstViewController viewWillAppear");
} - (void)viewDidAppear:(BOOL)animated
{
NSLog(@"FirstViewController viewDidAppear");
} - (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"FirstViewController viewWillDisappear");
} - (void)viewDidDisappear:(BOOL)animated
{
NSLog(@"FirstViewController viewDidDisappear");
} @end
SecondViewController.h + SecondViewController.m
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad
{
[super viewDidLoad]; NSLog(@"SecondViewController viewDidLoad");
} - (void)viewWillAppear:(BOOL)animated
{
NSLog(@"SecondViewController viewWillAppear");
} - (void)viewDidAppear:(BOOL)animated
{
NSLog(@"SecondViewController viewDidAppear");
} - (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"SecondViewController viewWillDisappear");
} - (void)viewDidDisappear:(BOOL)animated
{
NSLog(@"SecondViewController viewDidDisappear");
} @end
需要注意的地方:
1. 容器controller最好定义一个专门用来展示子controller相关view的区域,如例子中的,其中,masksToBounds很重要,要不然,整个controller都会被展示出来的.
self.showArea = [UIView new];
self.showArea.frame = CGRectMake(0, 0, 320, SCR_HEIGHT - downLenth - 10);
self.showArea.layer.masksToBounds = YES;
[self.view addSubview:_showArea];
[self.showArea addSubview:_firstVC.view];
2. 调用完addChildViewController之后还需要调用didMoveToParentViewController,官方文档里面有说明.
3. 为什么在点击一个按钮切换控制器的时候,showArea什么都不用设置,为何还能显示出变化呢?
其实这一点我也没弄明白为何呢.
4. 这个与UITabbarController的功能类似,都有懒加载功能,实际上可以用来当做模拟UITabbarController使用,具备更高自由度的定制Tabbar的功能.
使用addChildViewController手动控制UIViewController的切换的更多相关文章
- 使用 Storyboard Segue 实作 UIViewController 的切换
http://blog.csdn.net/mazhen1986/article/details/7791430 Storyboard 是在 iOS 5 SDK 中才出现的新名词,它其实就是原本的 Xi ...
- 【Xamarin 开发 IOS --使用 Storyboard Segue 实作 UIViewController 的切换 (实例)】
注意:在vs2015中进行画板之间的导航的时候,使用CTRL+鼠标左键进行导航的设定. 使用 NavigationController 进行 画板的链接.... 使用 Storyboard Segue ...
- UIVIewController自定义切换效果-b
之前介绍动画时提过UIView的转场动画,但是开发中我们碰到更多的viewController的切换,ios中常见的viewcontroller切换有四种:模态视图,导航栏控制器,UITabBar ...
- 试图切换控制addChildViewController、_transitionFromViewController
试图切换能够用transitionFromViewController. 步骤: View Controller中能够加入多个sub view,在须要的时候显示出来: 能够通过viewControll ...
- 自定义视图控制器切换(iOS)
在iOS开发过程中,通常我们会使用UINavigationController,UITabbarController等苹果提供的视图控制器来切换我们的视图.在iOS5之前,如果要自定义容器视图控制器很 ...
- UIViewController相关知识
title: UIViewController 相关知识date: 2015-12-13 11:50categories: IOS tags: UIViewController 小小程序猿我的博客:h ...
- Swift - UIViewController
UIViewController类详解: 通过Nib文件初始化 init(nibName nibName: String?, bundle nibBundle: NSBundle?) println( ...
- BSBuDeJie_01
一. 基本配置 1 项目图标 将图片直接拖入Assets-AppIcon 2 启动图片 3 软件名称 4 删除Main.stroryboard 5 设置窗口的根控制器 - (BOOL) ...
- UITabBarController底层实现
1.首先要了解:任何控制器,都能添加子控制器 UIViewController里面有一个方法: - (void)addChildViewController:(UIViewContr ...
随机推荐
- mvc的cshtml Request取不到值
如果路径为:http://localhost:2317/food/1,这时用Request["id"]是取不到值的应该用: Request.RequestContext.Route ...
- LoadRunner参数化取值与连接数据库
LoadRunner参数化取值与连接数据库 LoadRunner在使用参数化的时候,通常都是需要准备大数据量的,也因此LoadRunner提供两种参数化取值方式,一种是手动编辑,另一种就是通过连接 ...
- linux中shell,awk,sed截取字符串方法总结
转自:http://www.cnblogs.com/kinga/p/5772566.html Shell 第一种: ${parameter%word} 最小限度从后面截掉word${parameter ...
- Just a Hook (线段树)
给你n个数(初始时每个数的值为1),m个操作,每个操作把区间[l,r]里的数更新为c,问最后这n个数的和是多少. 区域更新用懒惰标记 #include<bits/stdc++.h> usi ...
- [漏洞复现] CVE-2017-11882 通杀所有Office版本
此漏洞是由Office软件里面的 [公式编辑器] 造成的,由于编辑器进程没有对名称长度进行校验,导致缓冲区溢出,攻击者通过构造特殊的字符,可以实现任意代码执行. 举个例子,如果黑客利用这个漏洞,构造带 ...
- 使用命令行管理virtualBox
最近在鼓捣hadoop,装了几台虚拟机,,总感觉gui启动很别扭,后来发现virtualBox有个headless模式,只想说舒服! 常用命令 VBoxManage startvm name|id - ...
- 【基础知识】C#数据库中主键类型的选择
主键在数据库中占有很大的地位,对于表的关联性,和数据的唯一识别性有重要的作用: 1,在C#开发中,Int自增字段和Guid(数据库中是uniqueidentifier类型)可设置为主键: 1>G ...
- 1015 Reversible Primes (20)(20 point(s))
problem A reversible prime in any number system is a prime whose "reverse" in that number ...
- 深入理解ajax系列第七篇
前面的话 虽然ajax全称是asynchronous javascript and XML.但目前使用ajax技术时,传递JSON已经成为事实上的标准.因为相较于XML而言,JSON简单且方便.本文将 ...
- MVC 设计模式与三层架构
一.JavaEE开发模式 什么是开发模式 模式是在开发过程中总结出的"套路",总结出的一套约定俗成的设计模式 JavaEE模式 model1模式 技术组成 :jsp+javaBea ...