In iOS development, the most commonly used ways to switch views are mainly as follows:
1. push、pop
Example of use (ViewController assumes that it is the controller that needs to jump):
[ pushViewController:ViewController animated:YES]; //Open the stack and jump to the specified controller view[ popViewControllerAnimated:YES]; //Pull the stack and return to the previous view[ popToViewController:ViewController animated:YES]; //Bulle stack Return to the view where the specified controller is located[ popToRootViewControllerAnimated:YES]; //Bullet stack Return to the root controller view
2. modal
Example of use (ViewController assumes that it is the controller that needs to jump):
[self presentViewController:ViewController animated:YES completion:nil]; //Skip to the view where the ViewController is located[self dismissModalViewControllerAnimated:YES]; //Exit the current view
3. Switch the rootViewController of the window
Example of use (ViewController assumes that it is the controller that needs to jump):
UIWindow *window = [UIApplication sharedApplication].keyWindow; //Get the main window = [[ViewController alloc] init]; //Set the root view of the main window to the current controller
4. Add another controller's view as a subview of the current controller view
[ addSubview: ]; //Commonly used when implementing complex interfaces
A brief introduction to the advantages and disadvantages of various methods:
1. Push and pop method
Advantages: Controller switching is reversible and the original graph will not be destroyed
Disadvantages: Relying on UINavigationController, it is necessary to have a navigation controller, which may conflict with certain interfaces that do not require navigation controllers.
The push operation is reversible, that is, it is reversible when switching the controller.
2、modal
Advantages: Controller switching is reversible and the original graph will not be destroyed
Disadvantages: There is no free push method for jumping, the dismiss method can only return in order, and cannot jump. From the previous modal to the next, the previous controller will not disappear and will be in memory all the time.
3. Switch the rootViewController of window
Advantages: Jumps are quick and can be destroyed. Because the strong reference of window is changed to the current controller, the previous controller will disappear (ARC environment)
Disadvantages: View jump irreversible
4. Add a controller's view to the current view
Advantages: Setting another controller as a property by one controller, freely reading views in other controllers, thus enabling complex UI interfaces
Disadvantages: Complex, not applicable in most occasions
The list is compared as follows:
Comparison items | push、pop | modal | Switch the root controller of the window | Add a subview |
Destroy the source view | no | no | yes | no |
Use occasions | There is a navigation controller that needs to be able to return to the previous view | Without a navigation controller, you can return to the previous view | New features or no need to retain the previous controller | The situation where complex interfaces need to be implemented |
Is there a navigation controller | yes | no | no | no |
Summarize
The view switching method has its pros and cons. You need to choose the method of use according to our needs. The above is the whole of this article. The level is limited. If there is any incorrectness, I hope you will criticize and correct me, learn together, and improve together!