试图切换能够用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:这种方法转换显示的子视图。

同一时候加入对应的动画。

以下以一个样例来说明这几个新方法:

以下具体介绍一下上述效果的实现:

  1. 创建项目,changeViewController。
  2. 加入对应的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的更多相关文章

  1. 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端子 ...

  2. 分段控制器UISegmentedControl的使用、同一个控制器中实现多个View的切换、addChildViewController等方法的使用

    本文先讲解简单的分段控制器UISegmentedControl的使用,然后具体讲解它最常使用的场景:同一个控制器中实现多个View的切换. 文章构思: 1.先直接讲解一张UI效果图的四种实现方式. 2 ...

  3. Linux 普通用户su命令切换控制

    1.编辑配置文件/etc/pam.d/su .将下面配置文件"#“去掉: # auth           required        pam_wheel.so use_uid 改成 a ...

  4. vue 组件 单选切换控制模板 v-bind-is

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...

  5. iOS开发自定义试图切换

    CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transition.timingFu ...

  6. Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。

    所有的视图都继承自 UIViewController,都使用 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nib ...

  7. app引导页(背景图片切换加各个页面动画效果)

    前言:不知不觉中又加班到了10点半,整个启动页面做了一天多的时间,一共有三个页面,每个页面都有动画效果,动画效果调试起来麻烦,既要跟ios统一,又要匹配各种不同的手机,然后产品经理还有可能在中途改需求 ...

  8. jquery图片播放切换插件

    点击这里查看效果可自定义数字样式和左右点击按钮 这个更好:移入按钮切换版本 更多图片轮播 以下是HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  9. 通过gdb跟踪进程调度分析进程切换的过程

    作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...

随机推荐

  1. [国家集训队]最长双回文串 (PAM)回文自动机

    Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #include <cstr ...

  2. axios 使用post方式传递参数,后端接受不到问题

    一.URLSearchParams var params = new URLSearchParams(); params.append('key1', 'value1'); //你要传给后台的参数值 ...

  3. Linux 操作基础(一) -- Shell 命令格式和元字符

    1 命令格式 cmd [-选项] [参数] 说明: • 最简单的Shell命令只有命令名,复杂的Shell命令可以有多个选项和参数 • 参数是文件也可以是目录,有些命令必须使用多个操作对象 • 并非所 ...

  4. 不要在.h文件中定义变量

    今天在头文件.h中初始化了一个数组和函数,在编译的时候提示这个数组和函数重新定义了,检查后发现,犯了一个致命的错误,在头文件中定义变量... 以下引用别人的一篇说明,警示自己. C语言作为一种结构化的 ...

  5. linux的一页是多大

    命令 getconf PAGESIZE 结果为4096,即一页=4096字节=4KB(注意是Byte,1B=8bit) 在使用mmap映射函数时,它的实际映射单位也是以页为单位的,即不过我们把MAP_ ...

  6. COGS——T 803. [USACO Hol10] 政党 || 1776: [Usaco2010 Hol]cowpol 奶牛政坛

    http://www.lydsy.com/JudgeOnline/problem.php?id=1776||http://cogs.pro/cogs/problem/problem.php?pid=8 ...

  7. 可替代google的各种搜索引擎

    http://www.aol.com http://www.duckduckgo.com http://www.gfsoso.com http://www.googlestable.com  http ...

  8. zzulioj--1805-- SC和ta的游泳池(简单几何)

    1805: SC和ta的游泳池 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 154  Solved: 43 SubmitStatusWeb Boar ...

  9. windows下远程链接虚拟机Linux下MySQL数据库问题处理

    参考解决:https://www.cnblogs.com/blogforly/p/5997553.html 今天远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现原因是MySQL对 ...

  10. UVa 202 Repeating Decimals【模拟】

    题意:输入整数a和b,输出a/b的循环小数以及循环节的长度 学习的这一篇 http://blog.csdn.net/mobius_strip/article/details/39870555 因为n% ...