文章目录

  • 前言
  • GitHubDemo
  • presentingViewController和presentedViewController
    • 文档描述
    • Xcode打印实验
    • 分析
  • 多次弹窗
    • 描述
    • Xcode实验
  • dismiss
    • 文档描述
    • 举个栗子
    • 总结
  • 参考文章

前言

  • present以及dismiss显然是iOS开发最先学到的东西了
  • 项目中出现了要dismiss回上上个viewController,在查找方法时,发现调用present方法的对象可以不是self
  • 显然我对于这两兄弟的理解不是很OK

GitHubDemo

  • present-dismiss-learning
  • 其中包括三个ViewController以及研究中做的一些打印工作

presentingViewController和presentedViewController

文档描述

// The view controller that was presented by this view controller or its nearest ancestor.
//此视图控制器或其最近祖先显示的视图控制器。
@property(nullable, nonatomic,readonly) UIViewController *presentedViewController  NS_AVAILABLE_IOS(5_0);// The view controller that presented this view controller (or its farthest ancestor.)
//显示此视图控制器(或其最远祖先)的视图控制器。
@property(nullable, nonatomic,readonly) UIViewController *presentingViewController NS_AVAILABLE_IOS(5_0);
  • 放了两段百度上的机翻,几乎没什么用,我们还是来看Xcode上的打印结果如何吧

Xcode打印实验

- (void)touchPresent {SecondViewController *secondViewController = [[SecondViewController alloc] init];NSLog(@"self--%p--", self);NSLog(@"next--%p--", secondViewController);NSLog(@"BeforeA:self.presentedViewController--%p--", self.presentedViewController);NSLog(@"BeforeA:self.presentingViewController--%p--", self.presentingViewController);[self presentViewController:secondViewController animated:YES completion:nil];NSLog(@"LaterA:self.presentedViewController--%p--", self.presentedViewController);NSLog(@"LaterA:self.presentingViewController--%p--", self.presentingViewController);
}
  • 这是一个在firstViewController中一个button的点击事件,我们在执行present前后分别打印self(即firstViewController的这两个属性)
  • 我们做两次打印试验,第一次是在First弹Second,第二次是在Second弹Third
firstViewController--0x7fe8a2c0cc50--
secondViewController--0x7fe8a2c0cfb0--
thirdViewController--0x7fe8a2f0eb60--BeforeA:self.presentedViewController--0x0--
BeforeA:self.presentingViewController--0x0--
LaterA:self.presentedViewController--0x7fe8a2c0cfb0--
LaterA:self.presentingViewController--0x0--BeforeB:self.presentedViewController--0x0--
BeforeB:self.presentingViewController--0x7fe8a2c0cc50--
LaterB:self.presentedViewController--0x7fe8a2f0eb60--
LaterB:self.presentingViewController--0x7fe8a2c0cc50--

分析

  • presentingViewController属性返回父节点,presentedViewController属性返回子节点,如果没有父节点或子节点,返回nil。
  • 不太理解怎么出现文档中的 or its nearest ancestor以及its farthest ancestor,希望有人知道能告诉我

多次弹窗

描述

  • 在这个例子中,我们用First弹Second,之后用Second弹Third,假如我们尝试用First弹Third回发生什么呢

Xcode实验

  • 我们使用属性传值,将first传给second,在弹third的时候由它来执行该方法
Warning: Attempt to present <ThirdViewController: 0x7fc014f06ad0> on <FirstViewController: 0x7fc014d02450> whose view is not in the window hierarchy!
  • Xcode也是毫不含糊的直接给你来发报错,同时,界面也不会有任何改变
  • 因此能执行present的必须得是self(或者说是最顶层的控制器),此外,不管是你新建一个还是想用之前的viewController去弹都是8⃣️行的

dismiss

文档描述

Discussion
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.If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method.The completion handler is called after the viewDidDisappear: method is called on the presented view controller.呈现视图控制器负责解除其呈现的视图控制器。如果您在呈现的视图控制器本身上调用此方法,uikit会要求呈现视图控制器处理解除。如果您连续显示多个视图控制器,从而构建一个显示的视图控制器堆栈,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器和堆栈中该子级以上的所有视图控制器。当发生这种情况时,只有最上面的视图以动画的方式被取消;任何中间视图控制器都只是从堆栈中删除。最上面的视图将使用其模态转换样式来消除,这可能与堆栈中较低的其他视图控制器使用的样式不同。如果要保留对视图控制器的PresentedView控制器的引用,请在调用此方法之前获取PresentedView控制器属性中的值。完成处理程序在viewdid消失后调用:方法在显示的视图控制器上调用。

举个栗子

  • 还是A,B,C三个ViewController,我们从A到B到C
  • 第一种情况:[C dismiss] 回到B
  • 第二种情况:[B dismiss] 回到B 直接子视图控制器C和堆栈中该子级以上的所有视图控制器都是C
  • 第三种情况:[A dismiss] 回到A 直接子视图控制器C和堆栈中该子级以上的所有视图控制器B C

总结

  • 直接子视图控制器就是本身,该子级以上的所有视图控制器是指调用者以上的所有视图控制器

  • 所以本质上还是一层层dismiss,只是只有剥除本身的是有动画效果的,因此我们看不到好想一层层剥下去的动画,好想是直接dismiss了一次就到位了,可以说是非常有迷惑性了

参考文章

  • iOS 聊聊present和dismiss

深入理解present以及dismiss相关推荐

  1. 自定义present和dismiss的转场动画

    趁周末闲暇之余,重新写一下present和push的自定义动画. 本来之前有写过一个,因为没有及时整理,到导致一时凌乱,找不到具体在哪了,提醒各位,平时要注意代码的整理和归档,不然到时候重复的代码写了 ...

  2. present 和Dismiss

    最近用到了模态视图,简单了解了下. 从VC1 present VC2 ,在从VC2 present VC3 VC1 就充当presenting view controller VC2就是present ...

  3. dismiss ios pop效果_iOS ~ ViewController的Push,Pop和Present,Dismiss转场动画

    转场动画涉及到的包括导航控制器的Push动画和Pop动画,以及普通控制器的Present和Dismiss动画,主要就是通过控制器遵守UIViewControllerTransitioningDeleg ...

  4. 【iOS开发】——present和push

    假期写UI的时候就遇到了到底是使用present还是使用push,二者虽都可以推出新界面,但是用法等方面略有不同. 一. present与push之间的不同点和相同点 共同点 push与present ...

  5. iOS present 和 push

    文章目录 简介 使用方法 A视图 present 到 B视图再push到 C视图 C视图使用dismiss返回 C视图使用pop返回 简介 共同点 – present和push方法都可用于推出新的界面 ...

  6. 【iOS】present和push

    present和push present和dismiss 使用方法 特点 push和pop 使用方法 特点 demo present和dismiss 使用方法 使用一般的视图控制器用present方法 ...

  7. iOS - present和push

    一.定义区别 pushViewController 导航栏控制器入栈的方式切换页面(pop可以返回任意一层)push一般用于同一业务不同界面之间的切换(也就是只能导航栏间的切换,从第二个导航栏到第三个 ...

  8. [iOS]-present和push

    共同点: present和朴实方法都可以用于推出新的界面,present和dismiss对应使用,push和pop对应使用. 不同点: present弹出的视图是模态视图(是一个临时视图)并且模式情况 ...

  9. (0030) iOS 开发之跳转之转场动画

    demo: 1 iOS  ~ ViewController的Push,Pop和Present,Dismiss转场动画 // http://blog.csdn.net/zhangao0086/artic ...

  10. iOS自定义转场动画实战讲解

    转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...

最新文章

  1. JS数组去重,JS根据数组里面的对象属性值去重
  2. 最新机器学习开源项目Top10
  3. 基于.NET实现数据挖掘--朴素贝叶斯算法
  4. docker 安装 minio
  5. 【Android】Context
  6. “ModSecurity2”源码分析
  7. Quick BI v3.0版本全新起航——2018杭州云栖大会
  8. vue 安装 less_解决旧Vue项目升级less-loader 6.0.0报错
  9. C语言10进制写法,用c语言编写函数Htoi(s)把由16进制数成10进制的数
  10. 1438.最小公倍数
  11. Golang中interface{}作为函数参数和函数返回值的使用
  12. Python3:递归实现输出目录下所有的文件
  13. Android上使用MP3格式录制声音
  14. 邮件服务端口 port 25、109、110、143、465、995、993
  15. KMS激活工具原地址
  16. matlab实现Sobel边缘检测
  17. 复选框不可编辑_你不可错过的Word操作文本小技巧 | 厉害了Word姐15
  18. eureka 自我保护机制
  19. linux系统可以安装浩辰CAD,浩辰CAD Linux下载
  20. 迪士尼鳄鱼洗澡背后的传奇人物:哥以前是干DJ的!

热门文章

  1. 输出方波c语言程序,产生锯齿波以及方波的C程序
  2. 人工智能是否会改写商业规则
  3. 双网卡 跃点_有关win7 pppoe拨号网络的跃点数及win7 多路internet访问带宽叠加 - Microsoft Community...
  4. 无线渗透(下)—企业级WPA破解
  5. 图像原点矩、二阶中心矩物理意义推导
  6. 树莓派33/100 - Pico控制直流小马达,为智能避障小车提供动力
  7. 控制台版单机双人五子棋游戏(Java版)---新人小白
  8. mysql修改密码椰子作用_全新椰子皮博客版本介绍及说明。
  9. 【Scratch 3.0游戏】—— 乒乓弹球游戏
  10. Python视频转换分辨率(附代码) | Python工具