IOS common mechanism in iPhone app development practice manual Study Notes (5)

Source: Internet
Author: User
1 Preface

In iOS development, I believe you have heard of delegation, data sources, targets, actions, and so on. Today we will simply learn about these contents.

2. Details 2.1 delegation and data sources

Have you ever wondered how to execute a task? It may be to repair a dishwasher or make up a pair of so. You can do it by yourself, or find someone to help you do what you don't know. This is the Commission.

The design mode of delegation allows your program to implement the interface defined by the system class. Working Method: introduce an object to another object that can answer any questions. By assigning a delegate, you can define related interfaces in the code to change the request or status.

If our object wants to be a delegate object, it needs to be implemented according to certain protocols. An agreement is like a contract. Many methods in the contract are optional, but other methods must be implemented.

For example, you can view a delegate class: uipickerview control, which is used to select values from the scroll list.

View the uipickerview document and we will find that the instance variables are defined as follows:

@ Property (nonatomic, assign) ID <uipickerviewdatasource> datasource;

This statement indicates that any class that supports the uipickerviewdelegate protocol (declared by ID) can use the uipickerview delegate.

2.2 Goals and operations

The data source is the data to be displayed. As a delegate, an instance variable named datasource must be implemented in the uipickerviewdatasource protocol.

@ Property (nonatomic, assign) ID <uipickerviewdelegate> delegate;

When processing the cocoa touch control, when you click the button and drag the slider to adjust the volume, these elements need to notify other parts of the application, and its status changes.

The target-Action design mode is used. In other words, you can set a target object for each control to receive notifications of changes. Like a delegate, you can select any object.

Unlike delegation, an action can define any method of an object. It only needs to comply with one of the following two signatures:

-(Ibaction) actionone {

}

-(Ibaction) actiontwo :( ID) sender {

}

Interface builder uses ibaction to determine the actions in the code. The second form contains a parameter, which is the object that initiates an action. The sender may be used to process the action.

2.2.1 manually add target objects and Methods

Every control in cocoa touch is a subclass of uicontroller. This class defines the following method:

-(Void) addtarget :( ID) target action :( SEL) Action forcontrolevents :( uicontrolevents) controlevents

This method has three parameters. The first is the target object, which is used to receive event activity notifications. The second parameter is the action parameter. The message defined by this parameter will be sent to the target object. The third is controlevents, Which is used with the specified event type to trigger the action.

For example, uicontroleventvaluechanged and uieventtouchupinside.

2.3 deep understanding of the life cycle of a view

The methods in the file use ibaction, and the properties are associated with the IB (interface builder) using iboutlet. For example:

@ Property (nonatomic, retain) iboutlet uibutton * mybutton;

They will be associated with the. XIB file. Her predecessor is the NIB file, short for "next interface builder". Because the new XIB file is based on XML, it is changed to XIB.

The loading mechanism of NiB uses accessors to set these instance method variables, and uses the object stored in the content to call-setmybutton: method.

The loaded object has all the settings you made in IB. If you change the background color of the view, this change will be recorded in the memory, and your instance variables will access it.

After an object is read from the NIB file, it will send an-awakefromnib message, but it cannot be used to initialize the information in this method. Because the view is not loaded yet, uiview uses the delayed loading technology, we can override the-viewdidload method.

Cocoa touch not only delays loading, but also automatically removes it when the memory is insufficient. The framework knows the currently displayed views and safely recycles the invisible views. During this process, it will send a message to the View Controller to let you know that day is insufficient.

-(Void) didreceivememorywarning method.

If our views depend on large cached information or other data that is easy to reconstruct, The didreceivememorywarning method is used to clear these objects in a good way.

You can override the-viewdidunload method and set our instance variables so that they are empty.

-(Void) viewdidunload {

Self. mybutton = nil;

[Super viewdidunload];

}

Here is a detail: the-viewdidload and-viewdidunload methods can be called multiple times to avoid the issue that initialization can only be executed once.

You do not have to use the NIB file to draw layers. You can override the-loadview method and add them to the main view using the-addsubview: method.

Finally, because the nib loader retains the interface objects stored in the memory, you need to do the last thing: release these views when trying to destroy the controller.

-(Void) dealloc {

[Mybutton release];

[Super dealloc];

}

3 conclusion

The above is all content and I hope it will help you.

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.