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!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM