试图切换能够用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. BZOJ 1355[Baltic2009]Radio Transmission(KMP)

    题意 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. (n<=1000000) 题解 这种求最小循环节的题一般是KMP. 因为有 ...

  2. POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)

    题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...

  3. 安装anaconda和tensorflow

    一.首先下载anaconda,下载:Anaconda2-4.3.1-Linux-x86_64.sh(https://repo.continuum.io/archive/)参考网址:https://ww ...

  4. Hdu4786

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. 从client(content=&quot;&lt;p&gt;&lt;/p&gt;&quot;)中检測到有潜在危急的 Request.Form 值。

    最近的站点要做一个新闻模块,站点后台须要对新闻进行管理,拿到富文本编辑器的内容,在获取的时候会报一个错误:从client(content="<p>.....</p>& ...

  6. Java transientkeyword使用小记

    1. transient的作用及用法 我们都知道一个对象仅仅要实现了Serilizable接口,这个对象就能够被序列化,java的这样的序列化模式为开发人员提供了非常多便利.我们能够不必关系详细序列化 ...

  7. Win 10最大的亮点不是免费而是人工智能

    7月27日,日本知名作家Manish Singh发表文章.题为"Eight Reasons Why You Should Upgrade to Windows 10",文中例举下面 ...

  8. zoj 3820 Building Fire Stations (二分+树的直径)

    Building Fire Stations Time Limit: 5 Seconds      Memory Limit: 131072 KB      Special Judge Marjar ...

  9. Eclipse中项目进行发布到Tomcat中的位置

    Eclips配置tomcat默认是发布到.metadate\plugins\目录下的,wtpwebapps.这样在实际的tomcat目录下,就找不到发布的项目.我们可以自己进行设置,在控制栏中找到se ...

  10. less01

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...