# DAY3 custom view, View Controller, View Controller specify view, loadView, viewDidLoad, MVC, screen rotation, memory warning,

Source: Internet
Author: User

# DAY3 custom view, View Controller, View Controller specify view, loadView, viewDidLoad, MVC, screen rotation, memory warning,

# DAY3 custom view, View Controller, View Controller specify view, loadView, viewDidLoad, MVC, screen rotation, memory warning

 

# Pragma mark ------- step for customizing the view -----------

To customize a view, follow these steps:

1) define the controls in the view and declare all controls as attributes.

2) override LTView to inherit the layout method of UIView (initWithFrame :) to create a subview and add a subview (define the layout frame of label and textField and add it to LTView) (note that you must add the child View to the parent view to open the space.) (here self = [super initWithFrame: frame];)

3) custom initialization method (defining the basic attributes of label and textField) (here is self = [self initWithFrame: frame];)

4) memory management (the reference count added by alloc when label and textField were created in step 2, or the reference count added by using the setter method)

 

# Pragma mark ------- View Controller -----------

View Controller:

View Controller Overview:

1) view controllers are an important bridge between application data and views. Each iOS app only displays one user interface, and the displayed content is coordinated and managed by controllers or a group of view controllers. Therefore, the View Controller provides a basic framework to build applications.

2) UIViewController is the parent class of all view controllers.

3) iOS provides many built-in view controllers to support standard user interfaces, such as navigation controller (UINavigationController), tag controller (UITabBarController), and table View Controller (UITableViewController.

 

View Controller functions:

1) controls View Size transformation, Layout View, and response events.

2) Detect and process memory warnings.

3) Detect and process screen rotation.

4) switch the inspection view.

5) implement module independence and improve reusability

 

Set the Root View Controller of window:

Step 1: Introduce the header file of the View Controller

Step 2: Define the window Root View in application: didFinishLaunchingWithOptions: Method

 

View Controller enabling:

Step 1: Define a subclass of UIViewController (Create A View Controller class MyUIViewController)

Step 2: Create a View Controller object (myViewController) in APPDelegate as the Root View Controller of the window (specify the Root View Controller of the window self. window. rootView = myViewController ;)

Step 3: Use the default created view object (self. view) (create a view in the viewDidLoad method and add it to the Root view (self. view) above)

 

Note: The default view (self. view) Layout (frame) on The view Controller cannot be changed)

Use init to initialize the default view of the View Controller:

UIView * view = [[UIView alloc] init];

View. backgroundColor = [UIColor redColor];

Self. view = view;

 

# Pragma mark ------- View Controller specify view -----------

View Controller:

1) custom View class inherits UIViw. Add a child view control to the initialization method.

2) rewrite the loadView upload method of the controller. Create a custom view object (alloc) and define it as the view of the controller. (Note: The loadView method is called when the Controller's view is nil to create a view programmatically. LoadView is the compile method called when the view is loaded for the first time when code is used to generate a view. It is used to implement controls using (write) code .)

3) set the child view control object to the property of the custom View class, and set in the viewDidLoad method: Add action, set delegate, and so on.

4) Add a button in the controller and click the event implementation and the proxy detection method.

 

# Pragma mark ------- loadView and viewDidLoad -----------

Notes for the loadView method:

1) loadView uses self. view this property, and self. view is called when it is nil to generate a valid self. view (this method must be rewritten to manually maintain views)

2) When the view Controller receives the didReceiveMemoryWarning message, the default implementation is to check whether the view of the current controller is in use. If its view is not in the current view hierarchy and your controller implements the loadView method, the view will be release, the loadView method is called again to create a new view. (Note: If loadView is not implemented below iOS6.0, viewDidUnload will not be called during memory warning)

 

Notes for rewriting the loadView method:

1) the method for loading the root view. In this method, we usually specify the root view as a desired view.

2) in the life cycle of a viewer, This method takes only once.

3) You cannot use the getter Method of self. view to obtain the Root view in the loading method, because the root view is being loaded and does not actually exist.

 

Considerations for the viewDidLoad method:

1) View Controller loading method. The default view controller is blank and the background color is transparent clearColor.

2) to display the content, create a view in this method and add it to the Root view (sefl. view ).

3) In general, we will perform initialization operations on the interface, such as adding some subviews to the view, loading model data from the database or network to the subview.

 

# Pragma mark ------- MVC -----------

MVC Overview:

1) UIViewController is the core of the MVC design pattern.

2) MVC is a framework-level design model.

3) M is a Model, mainly used to create a volume data Model (that is, the data structure ).

4) V is the View. All the controls we can see are the view. The main function of the view is to display data.

5) C is the controller, mainly controlling the communication between M and V.

 

# Pragma mark ------- screen rotation -----------

Screen rotation:

Check screen rotation:

The view controller can detect screen rotation. To handle screen rotation, you need to override the following methods:

1) supportedInterfaceOrientations (sets the orientation in which the device supports rotation. If not added, the View Controller cannot detect screen rotation ).

2) willRotateToInterfaceOrientation: duration: (pause music, close view interaction, etc. when rotating ).

3) willAnimateRotationToInterfaceOrientation: duration :( add Custom Animation, etc ).

4) didRotateFromInterfaceOrientation: (play music, open view interaction, etc ).

 

Other important methods of View Controller:

1) when the view size in the View Controller changes, the View Controller will receive a notification (that is, this method will be automatically executed at this time ):

ViewWillTransitionToSize: withTransitionCoordinator:

2) set the method supported during screen rotation and return the directions supported by all view controllers (note that the home key is not processed by default ):

SupportedInterfaceOrientations

/*

Note: The following enumerated values can be found in the API of this method.

// UIInterfaceOrientationMaskAll horizontal and vertical screens

// UIInterfaceOrientationMaskLandscape landscape Screen

*/

 

View processing:

1) Note that the view Controller automatically adjusts the view Size to adapt to screen rotation. The bounds is modified to trigger the layoutSubviews method of the view.

2) overwrite the layoutSubviews method of view and relay the layoutSubviews according to the device direction.

3) [UIApplication authentication application]. statusBarOrientation provides the current device direction (obtain the application status bar direction ).

 

// This method is executed as long as the bounds of the view changes.

-(Void) layoutSubviews {

// Obtain the UIInterfaceOrientation (Enumeration type) of the screen direction)

UIInterfaceOrientation orientaion = [UIApplication sharedApplication]. statusBarOrientation;

// Determine whether the image is horizontal

If (orientaion = UIInterfaceOrientationLandscapeLeft | orientaion = UIInterfaceOrientationLandscapeRight ){

Self. getbackButton. frame = CGRectMake (300,150,100, 40 );

} Else {

Self. getbackButton. frame = CGRectMake (150,150,100, 40 );

}

}

 

# Pragma mark ------- memory warning -----------

Memory warning:

The Memory available for each app in ios is limited. If the Memory used by an app exceeds this threshold, the system will send a Memory Warning message to the app. After receiving the message, the app must release as much memory as possible. Otherwise, the OS will close the app.

 

Note:

1) Use lazyCode)

2) viewDidLoad is called both when viewcontroller is created for the first time and when it is restored. Therefore, this function requires you to set the correct status according to different situations.

 

Http://blog.sina.com.cn/s/blog_65c178a80101gb66.html

 

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.