试图切换控制addChildViewController、_transitionFromViewController
试图切换能够用transitionFromViewController。
步骤:
View Controller中能够加入多个sub view,在须要的时候显示出来;
能够通过viewController(parent)中能够加入多个child viewController;来控制页面中的sub view。减少代码耦合度;
通过切换,能够显示不同的view;,替代之前的addSubView的管理
代码:
苹果API中所带方法:
addChildViewController:
removeFromParentViewController
transitionFromViewController:toViewController:duration:options:animations:completion:
willMoveToParentViewController:
didMoveToParentViewController:
以下具体介绍一下addChildViewController,一个ViewController能够加入多个子ViewController,可是这些子ViewControlle要想显示出来,须要把子controller的视图加入到父视图的对应位置r,能够通过transitionFromViewController:toViewController:duration:options:animations:completion:这种方法转换显示的子视图。
同一时候加入对应的动画。
以下以一个样例来说明这几个新方法:
以下具体介绍一下上述效果的实现:
- 创建项目,changeViewController。
- 加入对应的viewController,MainViewController、FirstViewController、SecondViewController、ThirdViewController。
3.把MainViewController加入到window中。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MainViewController *mainViewController=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.window.rootViewController=mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
4.在MainViewController中加入三个按钮。而且连接onClickbutton方法。
5.在MainViewController中加入三个子controller
#pragma mark – View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self addChildViewController:firstViewController];
SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self addChildViewController:secondViewController];
ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self addChildViewController:thirdViewController];
[contentView addSubview:thirdViewController.view];
currentViewController=thirdViewController;
}
当中要把当中的一个子controller的view加入到根视图中,这样才干显示出对应的视图。
6.点击button,切换视图。
-(IBAction)onClickbutton:(id)sender
{
FirstViewController *firstViewController=[self.childViewControllers objectAtIndex:0];
ThirdViewController *thirdViewController=[self.childViewControllers objectAtIndex:2];
SecondViewController *secondViewController=[self.childViewControllers objectAtIndex:1];
if ((currentViewController==firstViewController&&[sender tag]==1)||(currentViewController==secondViewController&&[sender tag]==2) ||(currentViewController==thirdViewController&&[sender tag]==3) ) {
return;
}
UIViewController *oldViewController=currentViewController;
switch ([sender tag]) {
case 1:
{
[self transitionFromViewController:currentViewController toViewController:firstViewController duration:4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
if (finished) {
currentViewController=firstViewController;
}else{
currentViewController=oldViewController;
}
}];
}
break;
case 2:
{
[self transitionFromViewController:currentViewController toViewController:secondViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
} completion:^(BOOL finished) {
if (finished) {
currentViewController=secondViewController;
}else{
currentViewController=oldViewController;
}
}];
}
break;
case 3:
{
NSLog(@"好友申请");
[self transitionFromViewController:currentViewController toViewController:thirdViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
} completion:^(BOOL finished) {
if (finished) {
currentViewController=thirdViewController;
}else{
currentViewController=oldViewController;
}
}];
}
break;
default:
break;
}
}
当中我把button设置成不同的tag了。
这时候点击button,就能够切换子视图了。
能够看到,这些view在没有使用时。是不会被load的,而且当有Memory Warning时。当前没有显示的view自己主动被unload掉了。
这样写的优点:
- 多个UIViewController之间切换能够加入动画
- 当内存警告的时候。能够把当前不是激活状态的ViewController内存释放。所以新的方法确实能有效地节省内存,也能方便地处理内存不足时的资源回收
- 能够把代码更好分开
试图切换控制addChildViewController、_transitionFromViewController的更多相关文章
- Atitit.软件控件and仪表盘(23)--多媒体子系统--视频输出切换控制cvbs av s-video Ypbpr pal ntsc
Atitit.软件控件and仪表盘(23)--多媒体子系统--视频输出切换控制cvbs av s-video Ypbpr pal ntsc 1. CVBS是AV接口 1 2. S-Video S端子 ...
- 分段控制器UISegmentedControl的使用、同一个控制器中实现多个View的切换、addChildViewController等方法的使用
本文先讲解简单的分段控制器UISegmentedControl的使用,然后具体讲解它最常使用的场景:同一个控制器中实现多个View的切换. 文章构思: 1.先直接讲解一张UI效果图的四种实现方式. 2 ...
- Linux 普通用户su命令切换控制
1.编辑配置文件/etc/pam.d/su .将下面配置文件"#“去掉: # auth required pam_wheel.so use_uid 改成 a ...
- vue 组件 单选切换控制模板 v-bind-is
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...
- iOS开发自定义试图切换
CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transition.timingFu ...
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。
所有的视图都继承自 UIViewController,都使用 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nib ...
- app引导页(背景图片切换加各个页面动画效果)
前言:不知不觉中又加班到了10点半,整个启动页面做了一天多的时间,一共有三个页面,每个页面都有动画效果,动画效果调试起来麻烦,既要跟ios统一,又要匹配各种不同的手机,然后产品经理还有可能在中途改需求 ...
- jquery图片播放切换插件
点击这里查看效果可自定义数字样式和左右点击按钮 这个更好:移入按钮切换版本 更多图片轮播 以下是HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 通过gdb跟踪进程调度分析进程切换的过程
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...
随机推荐
- shell编程, 100文钱买100只鸡, 简单实现
今天碰到一个有趣的问题: 群友用shell写的一个: #!/bin/bash # 百元买百鸡,每种鸡至少买一只 all= # 公鸡每只多少元 read -p '公鸡多少文一只: ' gongji # ...
- [luogu] P1772 [ZJOI2006]物流运输(动态规划,最短路)
P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...
- Mysql学习总结(21)——MySQL数据库常见面试题
1. 如何使用SELECT语句找到你正在运行的服务器的版本并打印出当前数据库的名称? 答:下面的语句的结果会显示服务器的版本和当前的数据库名称 mysql> SELECT VERSION(), ...
- Android 解决TextVIew载入自己定义字体慢的问题
网上非常多自己定义字体样式的代码.都是告诉应该这样做: 在自己定义控件里写,可是这样写有问题,会随着界面里自己定义控件越多.Activity载入速度越慢,太多了easy造成内存泄露问题,由于你没创建一 ...
- 可能是东半球最全的RxJava使用场景小结
一.Scheduler线程切换 这样的场景常常会在"后台线程取数据,主线程展示"的模式中看见 Observable.just(1, 2, 3, 4) .subscribeOn(Sc ...
- poj_3371
一道模拟题,写的有点麻烦 #include<iostream> #include<cstring> #include<cstdio> #include<alg ...
- 6.deque
#include <iostream> #include <deque> #include <algorithm> using namespace std; //序 ...
- Android控件postDelayed用法,View自带的定时器
有一个需求是这样的,点击加关注按钮后,执行关注操作,成功后按钮文字变为“已关注”,保持3秒,三秒后按钮文字便问“取消关注”,点击后执行取消关注的操作 源码: public boolean postDe ...
- 文本域内容在div中带换行显示
function ReplaceSeperator(mobiles) { var i; var result = ""; var c; for (i = 0; i < mob ...
- caffe(4) 激活层(Activation Layers)及参数
在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...