IOS mode view, switching between views, and ios Mode

Source: Internet
Author: User

IOS mode view, switching between views, and ios Mode

1. Simple switching between views.

Switching between views is actually switching between view controllers. Because the controllers attempt to have views on them, and the controllers attempt to switch over, the views naturally implement switching.

Create a project and create two classes: FirstViewController and SecondViewController. Their parent classes are both UIViewController and the ViewController that comes with the project. There are three view controllers in total.

In each class, that is, every attempt controller here writes a touch method as shown below. This method can be called by clicking the screen so that we can debug the program.

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {}

Set the background color of each view to facilitate debugging.

  

1. to switch from ViewController to FirstViewController, first introduce the header file of FirstViewController in ViewController.

Code in ViewController. m:

-(Void) viewDidLoad {

[Super viewDidLoad];

Self. view. bacgroundColor = [UIColor whiteColor]; // set it to white

}

// Click the screen to call this method

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {

FirstViewController * firstVC = [[FirsttViewController alloc] init];

[Self presentViewController: firstVC animated: YES completion: ^ {

NSLog (@ "present successful, switch from ViewController to FirstViewController"); // if this method is called, this line of code is executed.

}];

}

In this case, the ViewController will not be returned from FirstViewController. Therefore, you need to write the following method in FirstViewController. m. Introduce the ViewController header file in FirstViewController.

-(Void) viewDidLoad {

[Super viewDidLoad];

Self. view. bacgroundColor = [UIColor redColor]; // set it to red.

}

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {

[Self dismissViewControllerAnimated: YES completion: ^ {

NSLog (@ "dismiss succeeded, returned from FirstViewController to ViewController ");

}];

}

This enables conversion from ViewController to FirstViewController.

Conclusion: To switch between views, write the following two methods in the two view controllers.

PresentViewController: animated: completion:

DismissViewControllerAnimated: completion:

These two methods are generally used together to realize switching between two views. We will now describe a present method and a dismiss method.

 

2. Modal View

The view controller has a presentModalViewController: method. This method can be used to switch views from the current view to the next view. All the views popped up through this method are called modal views, modal view is very common on mobile phones. This method is as follows:

[Self presentModelViewController: animated: completion: ^ {}];

A mode view usually appears in a pop-up window, for example, a login window.

When the modal view is displayed, different animation effects are set through the modalTransitionStyle attribute.

Call dismissModalViewControllerAnimated to close the window.

The sample code is roughly the same as the above, except that different animation effects are set through the modalTransitionStyle attribute when the modal view is displayed. Make a slight modification to the method called above.

In ViewController. m

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {

FirstViewController * firstVC = [[FirstViewController alloc] init];

FirstVC. modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // set the animation effect when the view is popped up.

[Self presentModalViewController: firstVC animated: YES];

}

Then call it in FirstViewController. m.

[Self dismissModelViewControllerAnimated: completion: ^ {}];

 

3. In both cases, direct switching between controllers is achieved, and there is nothing else in the process. The following describes how to implement view switching if the navigation bar controller (UINavigationController) exists.

You can drag a UINavigationController in story. board, set its root View to ViewController, and check √ for the is initial View Controller in story. board so that the program entry enters from the navigation bar Controller.

  

Others are the same as those in 1. The call method is changed. If the navigation bar controller is available, the call method is as follows:

  

In ViewController. m

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {

FirstViewController * firstVC = [[FirstViewController alloc] init];

[Self. navigationController pushViewController: firstVC animated: YES];

}

In this way, even if no method is written in FirstViewController. m, the program runs with a button to return ViewController.

Click Back to automatically return to the upper level.

However, you can also write a return method. The system uses this method. The difference between writing and not writing is added below. I personally think this is quite important.

[Self. navigationController pop...]; we recommend you try it yourself. So the method used here is summarized as a push and a pop.

 

Supplement:

First, the UINavigationController has viewControllers, which is an array that stores all view controllers of the navigation bar controller, that is, all viewControllers of the navigationController are in viewControllers.

When FirstViewController is not pushed to the FirstViewController, it is not a sub-controller of navigationController. After being pushed to the FirstViewController, navigationController adds a new sub-controller FirstViewController. The FirstViewController is also available in viewControllers. If the pop method is not used, the navigationController of FirstViewController will exist, but the pop method will be released again, and the viewControllers of navigationControllerd will not have FirstViewController.

Note: the push and pop methods are both navigationController methods. Generally, [self. navigationController...] is used for calling.

  

This explains why a navigation bar controller has only one root controller, how other controllers are added, and how to remove them.

Finally, why didn't SenconedViewController be used? Keep yourself doing the experiment.

 

If you are a beginner in iOS, please kindly advise.

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.