Chapter 3 design mode of iOS

Source: Internet
Author: User

Target

IOS development framework

Common IOS design modes

IOS Program Status

IOS app lifecycle-UIApplication

IBOutlet IBAction in IOS development

Use of the IOS text box and its proxy class

 

1. IOS development framework

What is a development framework?

We pack common classes into a single library, which can be reused.

The Framework is an actual product.

 

What is the design model?

Provides a set of common, reusable, and tested solutions for common problems.

The design pattern is not a specific piece of fixed code. It can be understood as the "Total score" word written in the composition.

The design pattern is a complex.

 

What is the relationship between the development framework and the design model?

(1) The framework defines the overall structure of the application (class diagram)

(2) The framework defines the relationship between classes and objects.

(3) Reuse of frameworks

(4) Various design modes are usually used during framework design.

 

What are the benefits of design patterns?

The design pattern is not limited to a certain language. It is summarized from life, and applied to program development.

All over the world ---- MVC

 

2. Common IOS design modes

MVC

Model-View-Controller)

Delegate)

Delegate the responsibility of the implementation method to another different object so that the two objects can accomplish one thing together.

 

Monomer (Singleton)

Global Object

Only one

Other classes in the application can be accessed.

 

Category

It allows us to add a new method for an "existing class"

 

3. IOS Program Status

The resources of the IOS system are limited, and the status of applications in the frontend and backend is different. In the background, the program is subject to many system restrictions, which can improve battery usage and user experience.

 

To develop an app, follow the following guidelines of apple:

Application State
Application Status
Description
 
Not running
Not running
Program not started
 
Inactive
Not activated
The program runs on the foreground, but no events are received. The program usually stays in this state without event processing
 
Active
Activate
The program runs on the foreground and receives the event. This is also a normal front-end mode.
 
Backgroud
Background
The program is in the background and can execute code. When most programs enter this state, they will stay in this state for a while. After the time is reached, it will be Suspended ). Some programs can be in the Backgroud state for a long time after special requests
 
Sushortded
Pending
The program cannot execute code in the background. The system will automatically turn the program into this state and will not send a notification. When suspended, the program remains in the memory. When the system memory is low, the system clears the suspended program to provide more memory for the foreground program.
 

 

4. IOS app lifecycle-UIApplication

Check the main. m file in the IOS project and find the code in it is as follows:

# Import <UIKit/UIKit. h>

 

# Import "XXXAppDelegate. h"

 

Int main (int argc, char * argv [])

{

@ Autoreleasepool {

Return UIApplicationMain (argc, argv, nil, NSStringFromClass ([XXXAppDelegate class]);

}

}

 

The UIApplicationMain function is called only in the main () function. It creates a UIApplication object and a xxxDelegate object.

 

The complete code should be as follows:

Intmain (int argc, char * argv [])

{

@ Autoreleasepool {

Return UIApplicationMain (argc, argv, UIApplication, NSStringFromClass ([AppDelegate class]);

}

}

 

UIApplicationMain

(1) create a UIApplication object and a UIApplicationDelegate object

(2) set the event loop and start to process the event.

 

Example:

Meeting:-UIApplicationMain

Boss + Secretary

Boss: Representative of the entire company, opening and speaking in the meeting;-UIApplication

SECRETARY: Schedule the entire meeting (the life cycle of the Meeting)-xxxxDelegate

Learn about the Conference schedule (time, place, and topic).-NoRunning

Ask the boss for approval to attend the meeting;-Inactive

Prepare the meeting file;-Action

Contact clothing, food, and shelter;-background

Determine the meeting schedule;-susponded

Sort out the meeting minutes after the meeting ends;

 

Determine manpower and labor division.

UIApplicationMain (argc, argv, UIApplication, NSStringFromClass ([AppDelegate class]);

Meeting (xx, xx, boss, Secretary );

 

Describes all the objects involved in the program running. Some objects cannot be modified, and some objects can be modified.

Three design modes are embodied: MVC, proxy (Application Delegate), and Singleton (UIApplication, UIWindow, which cannot be modified. The whole program is created only once)


 

 

 

 

Application lifecycle defined in the Protocol UIApplicationDelegate

(1) didFinishLaunchingWithOptions-loaded, to be displayed soon

(2) applicationDidBecomeActive-enters the active state and is running

 

Application windows are expected to have a root view controller at the endof application launch

 

(3) applicationWillResignActive-release activity status

(4) applicationDidEnterBackground-release activity status, automatically enters the background

(5) applicationWillEnterForeground-enters the foreground status again from the background and automatically enters the Active status

 

 

 

// Start normally

------- 1 didfinishlaunchingwitexceptions only run once, ensure that the interface is loaded once

------- 5 applicationDidBecomeActive

// Press the Home Key

------- 2 applicationWillResignActive

------- 3 applicationDidEnterBackground

// Press the icon to restart

------- 4 applicationWillEnterForeground

------- 5 applicationDidBecomeActive

// Press the Home Key

------- 2 applicationWillResignActive

------- 3 applicationDidEnterBackground

// Delete the program in the background

-------- 6 applicationWillTerminate

 

5. IBOutlet IBAction in IOS development

In IOS development, the MVC design mode is used to create the View interface through IB (InterfaceBuilder) xxxViewController. xib.

If you want the components on the interface to be reflected after clicking it, you need to use code control.

XxxViewController. h xxxViewController. m controller.

 

Q: How do I know a certain amount or function in the Code pointing to a component on the interface?

Solution: Add two "identifiers" to the OC code"

IBOutlet associates components and code, and the code can control the "value" on the component ";

IBAction associates components and code, and the code can respond to the "event" of the button;

 

How to implement:

(1) first write the IBOutlet IBAction code in the control class, and then connect the code in the xib;

(2) Without writing code, you can use the IB "assistant" tool to connect directly and automatically generate an IBOutlet.

IBAction code (highly recommended)

 

 

6. Use of the IOS text box and its proxy class

The IOS Library provides two definitions for all UIKit components: Class Definition and Protocol definition. Text Box:

UITextField: provides all the attributes of the interface, limited to the interface

UITextFieldDelegate: defines the operations that can be performed on the text box.

 

Original API

The UITextFieldDelegate protocol defines themessages sent to a text field delegate as part of the sequence of editing itstext. All of the methods of this protocol are optional.

Translation

The UITextFieldDelegete Protocol defines the delegete message that is passed to a text box during text editing in order.

 

Protocol method description

 

-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {

// Return a BOOL value to specify whether to edit the text field in sequence.

Return YES;

}

 

-(Void) textFieldDidBeginEditing :( UITextField *) textField {

// Triggered when editing starts. The text field will become first responder.

[TextField becomeFirstResponder];

}

 

-(BOOL) textFieldShouldEndEditing :( UITextField *) textField {

// Return the BOOL value to specify whether to allow end editing of text fields. When the editing ends, the text field will give the first responder

// If you want to stop the text field from disappearing when the user finishes editing, NO can be returned.

// This is useful for some programs whose text fields must always be active, such as instant messages.

Return NO;

}

 

-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) rangereplacementString :( NSString *) string {

// This method is called when the user uses the automatic correction function to change the input text to the recommended text.

// This is especially useful for applications that want to add revocation options

// You can trace the last modification made to a field or make a log record for All edits for auditing purposes.

// NO can be returned to prevent text changes

// The parameter of this method has a nsange object, indicating the location of the modified text. The recommended text is also in it.

Return YES;

}

/////////////////////////////////////

// Restrict the length of characters entered by the user. If the length is exceeded, it is automatically truncated.

-(BOOL) textField :( UITextField *) textFieldshouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string

{

If ([textField. text length]> MAXLENGTH)

{

TextField. text = [textField. textsubstringToIndex: MAXLENGTH-1];

Return NO;

}

Return YES;

}

/////////////////////////////////////

 

-(BOOL) textFieldShouldClear :( UITextField *) textField {

// Return a BOOL value to indicate whether content can be cleared based on user requests

// You can specify conditions to allow content clearing.

Return YES;

}

-(BOOL) textFieldShouldReturn :( UITextField *) textField {

// Return a BOOL value indicating whether to allow editing to end when you press the Enter key

// If the resignFirstResponder method is allowed to be called, the edit is ended, and the keyboard is collapsed.

[TextField resignFirstResponder]; // check the meaning of the resign to understand this method.

Return YES;

}

 

 

--------------------------------

Exercise 1: master the application of UITextFieldDelegate and UITextField

(1) When the text field gets the focus, it sets the background color of the text field to green.

-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {

NSLog (@ "textFieldShouldBeginEditing :");

Return YES;

}

-(Void) textFieldDidBeginEditing :( UITextField *) textField {

NSLog (@ "textFieldDidBeginEditing :");

TextField. backgroundColor = [UIColor greenColor];

}

 

(2) textFieldShouldEndEditing is called before text field loses focus. In this example, textFieldShouldEndEditing is used to return the original color of the background color.

-(BOOL) textFieldShouldEndEditing :( UITextField *) textField {

NSLog (@ "textFieldShouldEndEditing :");

TextField. backgroundColor = [UIColor whiteColor];

Return YES;

}

-(Void) textFieldDidEndEditing :( UITextField *) textField {

NSLog (@ "textFieldDidEndEditing :");

}

(3) When you enter characters, you must monitor the length of the input characters, which must be greater than 10.

-(BOOL) textField :( UITextField *) textFieldshouldChangeTextInRange :( nsange) range replacementText :( NSString *) text {

NSCharacterSet * doneButtonCharacterSet = [NSCharacterSet newlineCharacterSet];

Nsange replacementTextRange = [text rangeOfCharacterFromSet: doneButtonCharacterSet];

NSUInteger location = replacementTextRange. location;

If (textView. text. length + text. length> 10 ){

If (location! = NSNotFound ){

[TextViewresignFirstResponder];

}

Return NO;

}

Else if (location! = NSNotFound ){

[TextViewresignFirstResponder];

Return NO;

}

Return YES;

}

 

The textField: shouldChangeCharactersInRange: replacementString method is called every time you enter a character on the keyboard before the character is displayed in text Field. In this method, you can easily locate the characters entered by the test user and restrict the user from entering specific characters. In the above code, I used the done key to hide the keyboard: Check whether the replacement text contains any newLineCharacterSet characters.

 

If one character comes from newLineCharacterSet, it indicates that the done button has been pressed, so the keyboard should be hidden. In addition, each time the user inputs the content, the length of the current text content in text Field is also checked. if the content is greater than 140 characters, NO is returned, in this way, the text Field can be limited to 10 characters.

 

 

Exercise 2: A component similar to the text box. UITextView allows users to input multiple lines of text. Please query the API by yourself. practice the same example above and carefully understand how UITextView works with UITextViewDelegate.

 

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.