Compare four ways of switching between common views in IOS development _ios

Source: Internet
Author: User
Tags advantage

In iOS development, the most common ways to switch views are as follows:

1. Push, pop

Use examples (Viewcontroller is assumed to be a controller that requires a jump):

[Self.navigationcontroller Pushviewcontroller:viewcontroller Animated:yes]; Into the stack, jump to the specified controller view
[Self.navigationcontroller popviewcontrolleranimated:yes];//stack, return to previous view
[ Self.navigationcontroller Poptoviewcontroller:viewcontroller Animated:yes]; Bounce Stack Returns the view of the specified controller
[Self.navigationcontroller poptorootviewcontrolleranimated:yes];//stack back to root controller view

2. Modal

Use examples (Viewcontroller is assumed to be a controller that requires a jump):

[Self Presentviewcontroller:viewcontroller animated:yes completion:nil]; Jump to Viewcontroller view
[self dismissmodalviewcontrolleranimated:yes];//exit Current View

3. Toggle Window Rootviewcontroller

Use examples (Viewcontroller is assumed to be a controller that requires a jump):

UIWindow *window = [UIApplication Sharedapplication].keywindow; Get main window
window.rootviewcontroller = [[Viewcontroller alloc] init];//Set the root view of the main window to the current controller

4. Add a view of another controller as a child view of the current controller view

[Self.view AddSubview:ViewController.View]; Common when implementing a complex interface

Briefly introduce the advantages and disadvantages of the various approaches:

1, push, pop mode

Advantage: The controller switch is reversible and the original image will not be destroyed

Disadvantage: Rely on uinavigationcontroller, so need to have a navigation controller, and some interfaces do not need navigation controller may conflict

The push operation is reversible, which is reversible when the controller is switched.

2, modal

Advantage: The controller switch is reversible and the original image will not be destroyed

Disadvantage: Jump does not push the way free, dismiss way can only return in order, can not jump, the previous one modal to the latter, the former controller will not disappear, has been in memory

3, switch window Rootviewcontroller

Advantages: Jump directly and quickly, and can destroy the previous controller, because the window's strong reference to the current controller, so the previous controller will disappear (ARC environment)

Disadvantage: View jump not reversible

4, add a controller's view to the current view above

Advantage: A controller to set another controller as a property, free to read the views of other controllers to achieve a complex UI interface

Disadvantage: Complex, most occasions do not apply

The list is compared as follows:

Compare items Push, pop Modal Toggle the root controller of a window Add a child view
Destroying the Source view Whether Whether Is Whether
Use occasion There is a navigation controller that needs to be able to return to the previous view Without a jump to the navigation controller, you can return to the previous view Version new feature or no need to preserve the previous controller Need to implement a complex interface
Do you have a navigation controller Is Whether Whether Whether

Summarize

View the way each has advantages and disadvantages, we need to choose the way to use according to our needs, the above is the whole of this article Rong, the level is limited, if there is not correct place, hope you criticize, common learning, and common improvement!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.