zoukankan      html  css  js  c++  java
  • iOS dismissViewControllerAnimated:completion:使用方法

    我们都知道dismissViewControllerAnimated:completion:方法是针对被present出来的控制器的,一般我们这样使用:在一个控制器中present另外一个控制器A,然后在A中执行dismissViewControllerAnimated:completion:让自己消失。

    在ViewController中:

    AViewController *av = [[AViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:av];
    [self presentViewController:nav animated:YES completion:nil];

    在AViewController中执行disimiss:

    [self dismissViewControllerAnimated:YES completion:nil];

    对于这种很常见的场景,这种用法完全没问题。但是对于复杂一点的场景,这种用法就有点苍白无力了,先举个稍微复杂一点点的例子:ViewController present AViewController,AViewController present BViewController,在BViewController执行完某事件之后需要返回ViewController。这个时候需要怎样做呢?如果在BViewController直接执行
    [self dismissViewControllerAnimated:YES completion:nil];的话,它只会将BViewController消失。

    这里你可能会想到通过其他方式拿到AViewController,然后调用AViewController的[self dismissViewControllerAnimated:YES completion:nil];。但是,场景再复杂一点,在执行完各种present和push之后,到达了XViewController,在XViewController中执行成功任务之后需要回到ViewController,这个时候怎么办呢?我们知道当前如果有被present出来的控制器的情况下,调用UINavigationController的popToRootViewControllerAnimated:是不起作用的。那么我们如何把这个流程中所有被present和push的控制器给销毁呢?笨一点的办法是回溯整个流程,判断哪些控制器需要dismiss,哪些控制器需要pop。但这种方式显然有点低效和难以控制,下面我们来看看到底该怎么使用dismissViewControllerAnimated:completion:

    我们先看看官方文档到底怎么讲的:

    Dismisses the view controller that was presented modally by the view controller.
    The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.
    If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

    可以简单归纳为两点:

    第一点:谁present出来的控制器,谁负责把它dismiss掉,但是如果你在被present出来的控制器中调用dismiss的话,UIKit会自动让它的presenting控制器(找到谁把它present出来的)去执行dismiss。

    第二点:如果你present了一系列的控制器,那么系统会把被present出来的控制器放在一个栈中,当处在底层的控制器执行dismiss的时候,在它之后被present出来的控制器都会被移除,只有栈顶上的控制器会有dismiss动画。

    另外补充相关的两点:

    第一点:当被present出来的控制器的modalPresentationStyle = UIModalPresentationFullScreen时,执行当前present事件的控制器必须是一个全屏控制器,如果当前执行的控制器不是一个全屏的控制器,它将在视图层级结构中找到一个全屏的父类控制器去执行present事件。也就是说如果A 执行present B,那么B.presentingViewController不一定是A。比如你当前的控制器A在导航nav中,A present B之后,实际上B.presentingViewController指向的是nav而不是A。

    第二点:self.presentingViewController,它指向的是把当前控制器present出来的控制器或者是把当前控制器的最上层的父类present出来的控制器。

    通过上面的文档介绍,我们可以看到在本文刚开始介绍的最简单的使用场景下(ViewController present AViewController),在AViewController中执行[self dismissViewControllerAnimated:YES completion:nil]和在ViewController中执行[self dismissViewControllerAnimated:YES completion:nil]效果是一样的,这一点是因为系统帮我们处理好了(因为系统判判AViewController当前没有present出来任何控制器,所以系统会找到它的presentingViewController,也就是ViewController来执行dismiss事件)。在复杂一点的情况下,比如我们要dismiss掉当前被present出来的控制器的话,我们就需要想办法拿到处在栈底的那个控制器,在这个控制器中执行[self dismissViewControllerAnimated:YES completion:nil]才行。

    那么很显然,执行[self dismissViewControllerAnimated:YES completion:nil]的流程是这样子的:

    在我们上面讲的复杂场景下,我们怎么一次性把当前present出来的控制都dismiss掉呢?可以通过下面的方式来查找到最顶层的presentingViewController(其实,通常是我们window的rootViewController)让它来执行dismiss就好了,剩下的工作可能就是处理一下导航中的控制器了。

    比如我们在经过各种present和push之后才到达的XViewController页面中执行如下代码:

    UIViewController *presentingVc = self.presentingViewController;
    while (presentingVc.presentingViewController) {
        presentingVc = vc.presentingViewController;
    }
    if(presentingVc){
        [presentingVc dismissViewControllerAnimated:YES completion:nil];
    }
  • 相关阅读:
    sql语句常考知识点总结
    服务器搭建
    软件测试面试题
    linux常用命令
    kibana常用查询删除语法
    python从kafka消费数据
    foxmail客户端,写邮件窗口弹不出来
    JMeter学习——测试文件下载
    python中取两个列表中不同的元素
    MySQL主从复制
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/9339783.html
Copyright © 2011-2022 走看看