1.showViewController

先看看说明:

You use this method to decouple the need to display a view controller from the process of actually presenting that view controller onscreen. Using this method, a view controller does not need to know whether it is embedded inside a navigation controller or split-view controller. It calls the same method for both. The UISplitViewController and UINavigationController classes override this method and handle the presentation according to their design. For example, a navigation controller overrides this method and uses it to push vc onto its navigation stack.从这段可以看出,这个函数并没有提供什么新功能,目的就是简化编程,省去判断一些条件。比如,会自动为nav controller有关的vc调用push,而为普通的view controller调用present函数。在splite vc 中使用会做什么动作?

The default implementation of this method calls the targetViewControllerForAction:sender: method to locate an object in the view controller hierarchy that overrides this method. It then calls the method on that target object, which displays the view controller in an appropriate way. If the targetViewControllerForAction:sender: method returns nil, this method uses the window’s root view controller to present vc modally.

You can override this method in custom view controllers to display vc yourself. Use this method to display vc in a primary context. For example, a container view controller might use this method to replace its primary child. Your implementation should adapt its behavior for both regular and compact environments. 这段说的是,可以在view controller子类重写这个方法,用来显示vc。这个其实就是为大家提供了一个指定的函数名,让大家把自定义的显示逻辑都放到这里来,这样的话,在storyboard中使用时,就会更方便直观了。

2.showDetailViewController 是为了UISplitViewController 而写的高级版showViewController,

 In a regular environment, the UISplitViewController class overrides this method and installs vc as its detail view controller; in a compact environment, the split view controller’s implementation of this method calls showViewController:sender: instead.

看看regular的截图,这里面,点击左边的item,触发的就是showDetailViewController事件:

点击左边的item,右边的detailVC 就会被更换掉,注意不是像UI tab controller那样,保存实例,是释放掉了。

在看看compact下的样子:

点击项目后,就会和调用showViewController函数一模一样。

最后,贴一篇外国人写的经验,看完他的问题,对这2个函数会有更多的认识。


The new  -[UIViewController targetViewControllerForAction:sender:] method (and its related methods,  -showViewController:sender: and  -showDetailViewController:sender: ) in iOS 8 takes a clever responder chain-based approach to showing view controllers in a size-class-adaptable way.

Basically, all  UIViewController instances respond to  -showViewController:sender: and  -showDetailViewController:sender: , but their implementations use  -targetViewControllerForAction:sender: to find the appropriate view controller to actually do the showing. In the case of  -showViewController:sender: , this is likely an enclosing UINavigationController , and in the case of  -showDetailViewController:sender: , this is likely an enclosing UISplitViewController .

But according to the documentation, this shouldn’t actually work.  -targetViewControllerForAction:sender: is documented to use  -canPerformAction:withSender: to determine an eligible target. That method, in turn, is documented to return  YES if the receiver responds to the given selector.

If that’s all true, then  -targetViewControllerForAction:sender: should always return  self when given @selector(showDetailViewController:sender:) ! But it clearly doesn’t—it returns an enclosing  UISplitViewController , if one exists. So what’s going on?

Well, the docs for  -targetViewControllerForAction:sender: lie.  -targetViewControllerForAction:sender: does indeed walk the view controller hierarchy, sending  -canPerformAction:withSender: on the way. But if a view controller returns  YES , it will then determine whether the instance’s method for that selector  is an override of a  UIViewControllerimplementation for the same selector  . If not, it keeps looking up the chain.

This is why  UISplitViewController is able to ensure it is returned for  -targetViewControllerForAction:@selector(showDetailViewController:sender:)instead of any intermediate view controllers between itself and the sender. It’s also why applications are able to mimic UIKit’s pattern with their own custom actions, which they wouldn’t if  UIViewController instead relied on a hardcoded list of selectors.

I’ve filed documentation feedback with Apple. But in the meantime, remember that -targetViewControllerForAction:sender: is smarter than it seems!

ios8 新增的 showViewController 和 showDetailViewController的更多相关文章

  1. ios8.1.2耗电情况严重的解决方法

    打开cydia,搜索ifile(威锋源,版本2.1.0-1).打开ifile,进入路径/Applications.里面有许多程序文件,选择适当的进行禁用(ifile可以禁用程序的活动而不完全删除它,这 ...

  2. UIAlertView、UIActionSheet兼容iOS8

    链接地址:http://blog.csdn.net/nextstudio/article/details/39959895?utm_source=tuicool 1.前言 iOS8新增了UIAlert ...

  3. iOS8使用TouchID

    iOS8新增了LocalAuthentication框架,用于TouchID的授权使用.亲測,眼下须要用户的设备支持指纹识别并已设置锁屏,并且实际測试过程中反馈比較慢.不能直接跟第三方账号passwo ...

  4. UIViewController所有API的学习。

    <欢迎大家加入iOS开发学习交流群:QQ529560119> /*      UIViewController is a generic controller base class tha ...

  5. iOS VIPER架构(三)

    路由是实现模块间解耦的一个有效工具.如果要进行组件化开发,路由是必不可少的一部分.目前iOS上绝大部分的路由工具都是基于URL匹配的,优缺点都很明显.这篇文章里将会给出一个更加原生和安全的设计,这个设 ...

  6. 缩放因子和UI设计

    一.PPI 1.像素密度PPI:PPI(Pixel Per Inch by diagonal):表示沿着对角线,每英寸所拥有的像素(Pixel)数目. 根据勾股定理(直角三角形两边为a和b,斜边为c, ...

  7. iOS权限问题

    判断相机权限: NSString *mediaType = AVMediaTypeVideo; AVAuthorizationStatus authStatus = [AVCaptureDevice ...

  8. iosiPhone屏幕尺寸、分辨率及适配

    iosiPhone屏幕尺寸.分辨率及适配     1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Fac ...

  9. iPhone屏幕尺寸、分辨率及适配

    转:http://blog.csdn.net/phunxm/article/details/42174937 目录(?)[+]   1.iPhone尺寸规格 设备 iPhone 宽 Width 高 H ...

随机推荐

  1. verilog阻塞与非阻塞的初步理解(一)

    通过两个模块来区别两者.测试平台:Modelsim altera 6.5b 阻塞模块: module blocking(clk,a,b,c); :] a; input clk; :] b,c; :] ...

  2. SQL Server2008 with(lock)用法

    参考资料: WITH (NOLOCK) 在sqlserver 中with(nolock)详解

  3. hdu4982 Goffi and Squary Partition (DFS解法)

    BestCoder Round #6 B http://acm.hdu.edu.cn/showproblem.php?pid=4982 Goffi and Squary Partition Time ...

  4. HTML5游戏设计与开发 小白7-9月的动态

    好久没有更新博客了,最近在努力修炼提升逼格,当然了还有个恶心的毕业论文... 当然啦...在写这个论文的时候也就是为了提升下自身的技术,毕竟我的公司也不是游戏公司,SO 我决定开发个手机游戏.然后考虑 ...

  5. PHP缓存机制Output Control详解

    开启OB缓存的方式有如下两种: 1. php.ini中开启 output_buffering = 4096 启用了此指令,那么每个PHP脚本都相当于一开始就调用了ob_start()函数,PHP5.5 ...

  6. 【Solr】数据库数据导入索引库

    目录 分析框图 配置数据库与solrconfig.xml 回到顶部 分析框图 框图画的粗糙!勿喷啊!勿喷啊! 回到顶部 配置数据库与solrconfig.xml Dataimport插件 可以批量把数 ...

  7. 不挣扎了,开始学习LINQ TO XML,进而来解析网页。

    找到了别人遇到和我一样的问题:http://ylad.codeplex.com/discussions/430095(英文) 一位叫做Mister Goodcat的提供了信息: Short answe ...

  8. 关于QT写注册表开机自启动

    注册表中权限: 1.HKEY_CURRENT_USER 2.HKEY_LOCAL_MACHINE 网上有很多帖子都是用的2,其实这样有违用户权限,而且如果不是管理员用户,会写入不成功! 代码如下: Q ...

  9. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  10. 【C++】递增递减操作符与指针的关系

    可以将递增与递减操作符用于指针和基本变量,将递增操作符用于指针时,将把指针的值增加其指向的数据类型占用的字节数,这种规则适用于对指针递增和递减. int arr[5] = {21,32,23,45,3 ...