zoukankan      html  css  js  c++  java
  • UIViewController之间的相互跳转

    一、最普通的视图控制器UIViewContoller

      一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能,有两种方法。

      1、通过方法 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion跳转

      相信很多人都用过这个方法,他是所有视图控制器对象都有的成员方法。基本绝大部分情况下,这个成员方法都可以正常使用,不过有些时候可能会使用失败,失败一般的原因都是,同一个视图控制器,在同一个时间,只能present一个另外的视图控制器,如果当前的VC已经present了,再次present一个VC时,就会提示失败,具体的失败提示在log里面有,我忘了就不说了,如果想继续present,就必须将原来present的控制器dismiss。

      说到这里,再延伸下控制器的两个可能很多人都没注意的两个只读属性:presentedViewController和presentingViewController,他们分别是被present的控制器和正在presenting的控制器。比如说, 控制器A和B,[A presentViewController B animated:YES completion:nil]; 那么A相对于B就是presentingViewController,B相对于A是presentedViewController,即这个时候  

        B.presentingViewController = A;

        A.presentedViewController = B;

      这两个属性,在有些时候,用起来是很方便的。比如说,现在有个C界面,C界面被显示出来,可能有两种情况,一是modal出来的,另外一种是push出来的,这时候就可以通过当前界面对象的presentingViewController属性来判断到底属于哪种情况,如果是nil,表示是UINavigationController对象push过来的,如果不是则是modal过来的。

      2、通过方法 - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender跳转

        如果代码要用这种方式,首先要创建一个UIStoryboardSegue对象,并给初始化相应的值。UIStoryboardSegue类有哪些方法和属性,去看看官方文档就明白了,我这里就不多说了。

        UIStoryboardSegue对象,提供了 跳转源界面,跳转目的界面,以及一个identifier也就是上面的identifier了,相信这么一说大家就知道这个类是干嘛的了。

        不过我没用代码这么写过,一般用这种方法跳转,我都是在storyboard里面直接根据所给的identifier来写的,也就是我们在storyboard中不同界面间拉的线(也就是UIStoryboardSegue)的属性中写的。

        这种方法同时也支持UINavigationController的跳转,不过跳转的模式为push了,它只能在当前视图控制器是UINavigationController时才能用。

      上述两种方式,都是通过 dismissViewControllerAnimated 来返回前一个界面的。

    二、导航控制器UINavigationController

      1、pushViewController    推出某个视图控制器

      需要注意的是,这个方法,是UINavigationController和其子类才有的方法,普通的控制器是没有的。 所以用得时候一般是某个aNavigationController pushViewcontroller或者self.navigationController pushViewController。

      2.通过上面所述的performSegueWithIdentifier方法跳转,就不多说了。

        

      由于UINavigationController是一个视图控制器的容器,他里面可能放了很多个控制器,所以返回的时候可以分为几种情况。

      A:弹出当前显示的界面,也就是返回到上个界面, popViewController(注意,当当前界面是根结面时,这个方法是不起作用的)。

      B:返回到控制器的根结面,popToRootViewController。

      C:跳转到这个视图控制器的中间的某个界面。popToViewController。用这种方式,就需要知道跳转到哪个界面了,获取需要跳转的界面的方式有很多,我一般是遍历UINavigationController的viewControllers数组,用iskindofclass方法来获取某个控制器对象再来跳转的。

    三、UITabBarController

      tabbar控制器,相信大家也用的很多,一般作为app的根界面视图控制器。其实与其说UITabBarController的界面跳转,不如说是界面切换,因为UITabBarController的界面跳转其实就是UITabBarController的viewControllers数组中的几个界面切换。只要设置好了UITabBarController的viewControllers数组,切面的切换基本就没我们什么事儿了。

    四、补充一点 :通过子界面实现同个控制器下的界面切换。

      我们开发中,可能会遇到某个界面比较复杂,要进行多个界面的切换,如果把这些界面切换全都放在该界面中,控制器代码非常臃肿不说,控制起来也比较麻烦,这个时候我建议用不同的控制器来表示不同的界面,然后将这些界面通过addChildViewController添加到控制器的子控制器中,然后通过系统提供的方法进行切换,至于这种方法怎么用,大家看下官方文档就知道了。

      这种方法通常会和UISegmentController配合使用。

    三种ViewController跳转的异同

    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

    MainVC *mainVC = [[MainVC alloc] init];
    
    [self presentViewController:mainVC animated:YES completion:nil];

    这种方式一般出现在需要使用者完成某件事情,如输入密码、增加资料等操作后,才能(回到跳转前的控制器)继续。例如系统的WIFI连接输入密码提示。默认动画是从下至上。

     

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

    这种方式一般是使用者浏览资料,继而可以前进到下一个页面或回到上一个页面。默认动画是从右至左。

     

    - (void)addChildViewController:(UIViewController *)childController

    这个方法出现在iOS5以后,通过它即使不使用NavigationController也能够实现view hierarchy。有以下优点:

    1.页面逻辑很清晰,相应的View对应相应的ViewController。
    2.当某个子View没有显示时,将不会被Load,减少了内存的使用。
    3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。

    复制代码
    #import "ViewController.h"
    #import "FirstVC.h"
    #import "SecondVC.h"
    #import "ThirdVC.h"
    
    @interface ViewController ()
    {
        FirstVC *firstVC;
        SecondVC *secondVC;
        ThirdVC *thirdVC;
    }
    
    @property (weak, nonatomic) IBOutlet UIView *contentView;
    
    @property (strong, nonatomic) UIViewController *currentVC;
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        firstVC = [[FirstVC alloc] init];
        secondVC = [[SecondVC alloc] init];
        thirdVC = [[ThirdVC alloc] init];
        
        [self addChildViewController:firstVC];
        [self addChildViewController:secondVC];
        [self addChildViewController:thirdVC];
        
        [self.contentView addSubview:thirdVC.view];
        
        self.currentVC = thirdVC;
    }
    
    - (IBAction)onClick:(id)sender {
        if(self.currentVC==firstVC && [sender tag]==1) {
            return;
        }
        
        if(self.currentVC==secondVC && [sender tag]==2) {
            return;
        }
        
        if(self.currentVC==thirdVC && [sender tag]==3) {
            return;
        }
        
        UIViewController *oldVC = self.currentVC;
        
        switch ([sender tag]) {
            case 1: {
                [self transitionFromViewController:self.currentVC toViewController:firstVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                    
                } completion:^(BOOL finished) {
                    if(finished) {
                        self.currentVC = firstVC;
                    }
                    else {
                        self.currentVC = oldVC;
                    }
                }];
            }
            break;
            case 2: {
                [self transitionFromViewController:self.currentVC toViewController:secondVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                    
                } completion:^(BOOL finished) {
                    if(finished) {
                        self.currentVC = secondVC;
                    }
                    else {
                        self.currentVC = oldVC;
                    }
                }];
            }
            break;
            case 3: {
                [self transitionFromViewController:self.currentVC toViewController:thirdVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                    
                } completion:^(BOOL finished) {
                    if(finished) {
                        self.currentVC = thirdVC;
                    }
                    else {
                        self.currentVC = oldVC;
                    }
                }];
            }
            break;
            default:
                break;
        }
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    疲劳原理
    golang中的 time 常用操作
    access与excel
    数据结构正式篇!初探!!
    数据结构复习之C语言malloc()动态分配内存概述
    C语言字符数组与字符串
    数据结构复习之C语言指针与结构体
    c语言数组
    数据结构
    C语言腾讯课堂(一)
  • 原文地址:https://www.cnblogs.com/worldtraveler/p/4570842.html
Copyright © 2011-2022 走看看