zoukankan      html  css  js  c++  java
  • 试图切换控制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:这种方法转换显示的子视图。

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

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

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

    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内存释放。所以新的方法确实能有效地节省内存,也能方便地处理内存不足时的资源回收
    • 能够把代码更好分开

  • 相关阅读:
    es 报错cannot allocate because allocation is not permitted to any of the nodes
    linux下获取软件源码包 centos/redhat, debian/ubuntu
    windows假死原因调查
    k8s-calico
    helm使用
    docker网络模式
    4、formula 法则、原则、数学公式
    powershell自动添加静态IP
    WDS部署Windows server2012初试
    2、puppet资源详解
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7054640.html
Copyright © 2011-2022 走看看