Concept of delegated data source in OC proxy Protocol

Source: Internet
Author: User

(Network Abstract)

Protocol in objc is equivalent to the Java interface, and delagate is the implementation class of the interface (callback in C is similar );


The data source means that the object follows the data storage protocol and can store and use data.

 

The Protocol indicates that the method can be implemented by any class. The Protocol itself is not a class. They simply define an interface that can be implemented by other objects. When your class implements a method in the protocol, that is, your class implements that protocol.


ObjectiveAnother extension of-C is called the protocol, which is very similar to interfaces in Java. Both publish an interface through a simple method declaration list, and any class can be implemented. Methods in the Protocol are called through messages sent by other class instances.


In iPhone development, how does object-C Support delegate?
A: Set proxy
Then you can implement the Protocol.
It is equivalent to implementing an interface in C ++ or an abstract method in Java.
In some classes, you can not only set proxies, but also set data sources for them (for example, some modal windows, such as uiactionsheet ).

Most of the time, you need to perform some operations on Class A in Class B. In this case, you need a proxy for Class B to complete, this is common and practical in applications.

 

Source http://www.cocoachina.com/bbs/read.php? Tid-26591.html

Basic iPhone development knowledge ------ degegate)
As the name suggests, the delegate proxy delegates the tasks to be done by an object to other objects. So other objects are the object'sProxyTo take care of what you want to do. Reflected in the program,
First, we need to determine which object the principal is and what the principal is doing. The delegation mechanism is used in many languages. This is just a general idea. There will be many introductions on the Internet.
In the development process of Apple, the idea of implementing the delegated program is as follows. I will give an example of how to transmit information between views. For example, on two pages (uiiview
Graph object) to achieve value transfer, with the delegate (delegate) can be very good.
Method:
@ Interface A: uiview
Id transparendvaluedelegate;
@ Property (nomatic, retain) ID transparendvaluedelegate;
@ End
@ Implemtion
@ Synthesize transparendvaluedelegate

-(Void) Function
{
Nsstring * value = @ "hello ";
[Transparendvaluedelegate transparendvalue: value];
}
@ End

Class B
@ Interface B: uiview
Nsstring * value;
@ End
@ Implemtion B
-(Void) transparendvalue :( nsstring *) fromvalue
{
Value = fromvalue;
Nslog (@ "the value is % @", value );
}
@ End

// Set the delegate object of proxy a as B below
// Define Class A and Class B objects:
A * A = [[A alloc] init];
B * B = [[B alloc] init];
A. transparendvaluedelegate = B; // you can specify B as the delegate object of proxy.
In this way, values can be passed through delegation between view a and View B.

The following example shows two types of delegation:
1. For a view objectProxyThe object is the parent view, and the Child view usesProxyReal
Now let the parent View display other child views
2. One child view under the same parent view is another child ViewProxyObject, let another sub-view change its background color to the given color


ProxySolve the problem in C ++, and inherit from the system without moving it. It is okay to implement a protocol.
For example, in MFC, every program must inherit from cwinapp, which is nothing more than rewriting those virtual functions. Isn't that troublesome?
Therefore, in cocoa, A uiapplicationdelegate proxy is implemented without inheritance. You only need to care about
You can capture the events (functions) in the application lifecycle.

ProxyIs a reference pointer. For example, if Class A has a class B pointer, B isProxy,ProxyThat is, B. When a finishes one task, B must be told to call the corresponding method for response .. This isProxy

Protocol(Protocol) usage and code example

ProtocolIs a set of agreements between computer users after communication through the network. Two classes are used for communication.ProtocolIt is more convenient. The cocoachina moderator angellixf is a new handwrittenProtocolIntroduction and code examples to help beginners
I. Description
1. The Protocol declares methods that can be implemented by any class.
2. The protocol is not a class. It defines an interface that can be implemented by other objects.
3. If you implementProtocolA Method in, that is, this class implementsProtocol.
4. Protocols are often used to implement delegate objects. A delegate object is a special object used to collaborate or represent other objects.
5: delegate is to call the method defined by yourself and implement it using other classes.
6. New Features
@ Optional pre-compiled command: indicates the implementation method that can be selected.
@ Required: indicates the method that must be implemented forcibly.

Ii. Definition

. H
@ Protocol contactctrldelegate
-(Void) dismisscontactsctrl;
@ End

@ Interface contactsctrl: uiviewcontroller {
ID <contactctrldelegate> delegate;
}
@ Property (nonatomic, assign) ID <contactctrldelegate> delegate;

. M
@ Synthesize delegate;

Iii. Example

Example: uitextview
@ Protocol uitextviewdelegate <nsobject>

@ Optional

-(Bool) textviewshouldbeginediting :( uitextview *) textview;
-(Bool) textviewshouldendediting :( uitextview *) textview;

-(Void) textviewdidbeginediting :( uitextview *) textview;
-(Void) textviewdidendediting :( uitextview *) textview;

-(Bool) textview :( uitextview *) textview shouldchangetextinrange :( nsange) range replacementtext :( nsstring *) text;
-(Void) textviewdidchange :( uitextview *) textview;

-(Void) textviewdidchangeselection :( uitextview *) textview;

@ End

To call these methods, you must set the delegate of uitextview: textview. Delegate = self;

Iv. Demo

1. contactsctrl. h

# Import <uikit/uikit. h>

// DefineProtocol
@ Protocol contactctrldelegate
-(Void) dismisscontactsctrl;
@ End

@ Interface contactsctrl: uiviewcontroller {
Iboutlet uinavigationbar * contactnavbar;
ID <contactctrldelegate> delegate;
}
@ Property (nonatomic, assign) ID <contactctrldelegate> delegate;

-(Ibaction) cancelbtn :( ID) sender;
@ End

2. contactsctrl. m

@ Implementation contactsctrl
@ Synthesize delegate;

// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload {
[Super viewdidload];
Contactnavbar. topitem. Prompt = @ "select a contact to send a text message ";
}

// CallProtocolMethod in
-(Ibaction) cancelbtn :( ID) sender {
[Delegate dismisscontactsctrl];
}

3. protocoldemoctrl. h

# Import <uikit/uikit. h>
# Import "contactsctrl. H"
@ Interface protocoldemoctrl: uiviewcontroller <contactctrldelegate> {// Add a delegate
Contactsctrl * contactsview;
}

4. protocoldemoctrl. m

# Import "protocoldemoctrl. H"
# Define barbuttonadd (selector) [[[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemadd target: Self action: Selector] autorelease];

@ Implementation protocoldemoctrl
@ Synthesize contactsview;

// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload {
[Super viewdidload];
Self. navigationitem. rightbarbuttonitem = barbuttonadd (@ selector (addcontactaction :));
}

-(Void) addcontactaction :( ID) sender {
Contactsctrl * contactview = [[contactsctrl alloc] initwithnibname: @ "contactsctrl" Bundle: Nil];
Self. contactsview = contactview;
Contactsview. Delegate = self; // sets the delegate
[Self presentmodalviewcontroller: contactsview animated: Yes];
[Contactview release];
}

// Implement methods in the contactctrldelegate Protocol
-(Void) dismisscontactsctrl {
[Contactsview dismissmodalviewcontrolleranimated: Yes];
}

Let's take a look at the implementation of the demo. You can also take a look at objective-C basic tutorial 2.0. (For beginners)

UseProtocolIt is equivalent to sending a message to the programmer who reads the class declaration, indicating that we can use it to accomplish two things. What?
1. Self-encoding and decoding
2. Ability to copy itself

If you do not define it, the func in Step 4 is your method insteadProtocolIn

AndProtocolYou don't know how to implement it. You know that.ProtocolDivided into formalProtocolAnd informalProtocol, Officially adopt the implementation method

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.