Page jump and pop up modes for ViewController in iOS development

  • 2020-05-12 06:15:43
  • OfStack

ViewController page jump
When jumping from one Controller to another Controller, there are two types of 1:
1. Use UINavigationController to call pushViewController to jump; In this way, Controller is managed by pressing and pushing. Call the popViewControllerAnimated method to return.


    PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
    [self.navigationController pushViewController: ickImageViewController animated:true];
    [ickImageViewController release];


2. Jump using presentModalViewController of UIViewController itself; Call the dismissModalViewControllerAnimated method to return.


    PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
    [self presentModalViewController:ickImageViewController animated:YES];
// return
[self dismissModalViewControllerAnimated:YES];


Present ViewController Modally

1. Main USES

The pop-up mode ViewController is one of the most useful technologies in the IOS transformation. UIKit provides a number of ViewController, such as UIImagePickerController, specifically for modal display. Pop-up mode ViewController is mainly used in the following situations:

1. Collect user input information

2. Temporarily present 1 content

3. Change the working mode temporarily

4. Changes in the direction of the corresponding equipment (used in the case of two ViewController in different directions)

5. Display a new view level

These situations will temporarily interrupt the normal execution of the program, the main purpose is to collect or display 1 information.

2. Several concepts and common Settings

1. presenting view controller Vs presented view controller

When we modally display view controller B in view controller A, A ACTS as presenting view controller (popup VC), and B is presented view controller (popup VC). The official document suggests the interaction between the two through delegate. If you have used UIImagePickerController to select or take photos from the system album, you can find that the interaction between imagePickerController and the VC that pops it out is realized through UIImagePickerControllerDelegate. Therefore, in practical application, it is better to follow this principle and define delegate in the pop-up VC, and then implement the proxy in the pop-up VC, so as to facilitate the interaction between the two.

2. Modal Presentation Styles (pop-up style)

By setting the modalPresentationStyle property of presenting VC, we can set the style when View Controller pops up. There are four styles, which are defined as follows:

 
typedef enum {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet,
    UIModalPresentationFormSheet,
    UIModalPresentationCurrentContext,
} UIModalPresentationStyle;

UIModalPresentationFullScreen means that when VC pops up, presented VC is full screen. If VC wantsFullScreenLayout is set to YES, it will be filled under the status bar. Otherwise, it will not be filled under the status bar.

The height of presented VC is the same as the height of the current screen. The width is the same as the width of the screen in portrait mode. The remaining uncovered area will become dark and prevent the user from clicking.

In UIModalPresentationFormSheet mode, the height and width of presented VC are smaller than the screen size. presented VC is centered, leaving dark areas for 4 weeks.

In UIModalPresentationCurrentContext mode, presented VC pops up in the same way that presenting VC's parent VC does.

All four are valid on iPad, but UIModalPresentationFullScreen and iPod touch are always presented VC in UIModalPresentationFullScreen mode.

3. Modal Transition Style (pop-up animation style)

By setting presented VC modalTransitionStyle property, we can set the style of scene switching animation when presented VC pops up, which is defined as follows:

typedef enum {
        UIModalTransitionStyleCoverVertical = 0,
        UIModalTransitionStyleFlipHorizontal,
        UIModalTransitionStyleCrossDissolve,
        UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

We can see that there are four styles to choose from: slide in from the bottom, flip in horizontally, cross dissolve and page turn. These four styles are not restricted by the device, that is, either iPhone or iPad will display the transition effect according to the style we specify.

4. Dismiss Modal ViewController (disappear pop-up VC)

To eliminate presented VC, we can do this by calling either of the following two functions

dismissModalViewControllerAnimated:                 // Will be abandoned, do not agree to continue to use
dismissViewControllerAnimated:completion:

Who calls this disappearing presented VC method: the correct approach is "who pollinates who governs", that is, presenting VC calls the above method to cancel the display of presented VC. One advantage of this is that if an VC really doesn't want different choices made by the user, different view controller may pop up. When the view controller is no longer needed to be displayed, a direct call to [self controller] will make it disappear, regardless of the specific type of view controller it displays. Of course, the system is optimized here. When we call the above method in presented VC, the system will automatically pass the message to the corresponding presenting VC. In this way, we can realize the function that whoever pops out of presenting will disappear when it is no longer needed. If presented VC needs to have data transfer with presenting VC, it is recommended to use view controller in presenting VC implementation of the proxy function dismiss view controller.


Related articles: