Uipresentationcontroller (i) new features of PopOver, alert, action sheet and search
- Liu Bing published in the test? Look at me.
Liu Bing
The topic test of this article was included in the July 02, 2014 issue. Look at me.
A test staff is very small, the world is big, let us share unlimited! Learn something useful:)
+ Subscribe More topics included in this article
Uipresentationcontroller is a class that provides advanced view switching. It makes the process of managing present viewcontroller easier.
First of all, some presentation basics, on the ipad Settings page, you can pop a uiviewcontroller through popover, this pop-up, The controller that can interact with the user is called Presentedviewcontroller, and the Uiviewcontroller, which is partially obscured, is called Presentingviewcontroller, In Uipresentationcontroller, Presentedviewcontroller is the content of presentation, and Presentingviewcontroller is called Chrome. Such as:
Presentedviewcontroller and Presentingviewcontrollercontent
Chrome
All the presentation of Uiviewcontroller are managed by Uipresentationcontroller. In Uipresentationcontroller, you can define the content and chrome animations, you can change the content size according to the size of the change, you can vary the system, to change the way of display, Uipresentationcontroller is also reusable and can be easily used in other uiviewcontroller.
Uipopoverpresentationcontroller
It replaces the Uipopovercontroller in IOS8, which is functionally identical to the old controller, and adds some built-in adaptation features that can be automatically adapted to the ipad and iphone. The following is a comparison of the new version with the older interface:
Uipopovercontroller How to use:
We first declare a uiviewcontroller as a content controller, use it to initialize a uipopovercontroller, and then call the Presentpopover method to show it. This is the usage on the ipad. If you want to create a common method on the ipad and iphone, you need the following code:
We need to determine if the device is an ipad, and if it's an ipad, create a uipopovercontroller and show it, if not the ipad, Then you need to call the general Presentviewcontroller method to show a uiviewcontroller.
However, if we use Uipopoverpresentationcontroller, then we no longer need to judge the device:
The Modalpresentationstyle of Uiviewcontroller is set to Uimodalpresentationpopover, which is used to achieve popover effect, and each platform adapts automatically. The second line, through the Popoverpresentationcontroller property, gets its popoverpresentationcontroller instead of creating a new one. Then set some of its interface properties, and finally call the Presentviewcontroller method to display the controller. This allows the ipad and iphone to display the PopOver effect of auto-adaptation as shown in:
ipad effects iphone Effect
Visible, on the iphone, just as an ordinary Uitableviewcontroller show out.
The adaptive on iphone is implemented in delegate:
In the first method, the Uimodelpresentationfullscreen style is specified to show the controller. In the second method, we wrap the presentedviewcontroller using Uinavigationcontroller, which allows the content to be displayed through some of the methods provided by Navigationcontroller after an item is selected. or back.
Uialertview and Uiactionsheet
Uialertview and Uiactionsheet are inherited from the UIView, but when they are implemented, they use some uiviewcontroller way, and its interface is older, using the delegate way. In IOS8, a new uialertcontroller, which can implement both alert and action Sheets, and the interface in block mode, will be displayed in the same window of the application, not in the new window, It also has the same implementation as the previous PopOver controller, shown through Presentviewcontroller. Here's how to use the new Uialertcontroller:
First create a uialertcontroller, then add some action by Addaction method, and Uialertaction use block to set the button's click-handling method. Finally, the Uialertcontroller is displayed by calling Presentviewcontroller.
Uisearchdisplaycontroller
Search has two parts in iOS: Uisearchbar and Uisearchdisplaycontroller. They are very old interface, function and style are not satisfied with the status of the application, Uisearchdisplaycontroller configuration items are very few, it can only display the results in TableView, but not in CollectionView or Mapview display. Its present process simply adds the view as a sub-view by Addsubview. And if its parent view is a scrollview, then gesture processing will conflict. In IOS8, the introduction of Uisearchcontroller, which allows Presentingcontroller to be of any type, is not necessarily full-screen, search bar animation can be customized and can be adapted on different platforms.
In the previous interface, we used Uisearchdisplaycontroller to implement the search feature:
First initialize a uisearchbar, then use it to initialize a uisearchdisplaycontroller, specify the controller's Searchresuledatasource and Searchresultdelegate, The Searchbar is then displayed as the Tableheaderview of the TableView in the current view. And in iOS8, the implementation is this way:
Start by initializing a custom resultscontroller, which can be a collectionview or some other custom style. Then use this resultscontroller to create a uisearchcontroller, specifying Searchcontroller's searchresultsupdater as Resultscontroller, The Searchbar is then added to the TableView Tableheaderview, and finally, the definespresentationcontext of the current controller is set to Yes, Indicates that the current controller can be overwritten when Uisearchcontroller is present.
With this modification, all of them become controllers, no longer have uiview components, and no longer need to create new windows to show UIView, which is easier to control. Uipresentationcontroller provides a good abstraction for content and chrome, and the auto-fit between ipad and iphone makes the coding more concise.
IOS8 new features Uipresentationcontroller (i)