• ios8 新增的 showViewController 和 showDetailViewController


    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!

  • 相关阅读:
    【Manacher(马拉车)算法】
    【可持久化数据结构】
    react-dva修改默认端口的方法
    关于React Hooks使用
    react hook useContext 跨文件接收
    el-select中使用el-tooltip时,在下拉滚动时整个网页会出现滚动条,并抖动
    react的onClick执行函数和bind(this)问题
    禁用h5页面中长按图片弹出的弹层
    JS树结构操作:查找、遍历、筛选、树结构和列表结构相互转换
    js树结构查找节点
  • 原文地址:https://www.cnblogs.com/breezemist/p/5174807.html
Copyright © 2020-2023  润新知