Protocols and delegation in IOS

Source: Internet
Author: User

The Protocol is equivalent to the pure virtual base class in C ++. It can only define functions and can only be implemented by other classes. The delegation is similar to the Java interface. In fact, there is no inevitable relationship between the Protocol and the delegation, but the commonly used protocol in obj-C is used to implement the delegation.

Protocol-the protocol is used to follow the protocol, and the implementation method required by the Protocol must be implemented.

Delegate-delegation, as its name implies, is to entrust others to handle affairs, that is, when one thing happens, it will not be handled by others.

Note the following:

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 a method in the Protocol is implemented in a class, that is, the Protocol implemented by the class.
4. Protocols are often used to implement delegate objects.

5. Attribute keywords in the Protocol declaration:

@ Optional pre-compiled command: indicates the implementation method that can be selected.

@ Required: indicates the method that must be implemented forcibly.

The following example shows that a view contains B view, and B view needs to modify the interface of a view. At this time, delegates are required.

The procedure is as follows:

1. First define an agreement

2. Methods of a view Implementation Protocol

3. B View sets a delegate variable.

4. Set the delegate variable of B view to aview, which means that B view entrusts a view to do things.

5. After an event occurs, use the delegate variable to call the Protocol method in a view.

The code can be similar:

B _view.h: @ protocol B _viewdelegate <nsobject> @ optional-(void) touch :( ID) sender; @ end @ interface B _view: uiviewcontroller {} @ property (nonatomic, retain) ID <B _viewdelegate> touchdelegate; // declare the delegate as an alias @ endb_view.m: @ synthesize touchdelegate;-(ID) initwithframe :( cgrect) frame {If (Self = [Super initwithframe: frame]) {[touchdelegate touch]; // call the Protocol delegate and delegate it to a_view to implement the function} return self ;}@ enda_view.h: @ interface: uiviewcontroller <B _viewdelegate> {B _view * myb_view;} @ enda_view.m:-(void) viewwillappear :( bool) animated {myb_view.touchdelegate = self; // sets the delegate, here we can see that the delegate can also be understood as an attribute in B _view [self. view addsubview: myb_view];}-(void) ontouch :( ID) sender {// Implementation Protocol Method
}

As mentioned above, the protocol and delegation are not necessarily related, but in obj-C, we often use the Protocol to implement delegation, but we can also implement delegation without the protocol. For example:

Define a Class:

@interface A:NSObject-(void)print;@end@implement A-(void)print{NSLog(“ok,print”);}

Defines a class B and declares in B that the instance of a is a member variable of B:

@interface B:NSObject{   A *a_delegate;}@end

Then implement the delegate mechanism in the main () function:

Void main {B * B = [[B alloc] init]; A * A = [[A alloc] init]; B. a_delegate = A; // set the member variable in B to A, so that the print method in a can be called. [B. a_delegate print];}

There is another way, which is similar to the Protocol we use to implement the delegate mechanism, but it does not use the Protocol:

First, define a class B and class B delegate Class A to implement print:

@interface B:NSObject{  id delegate;}  @end @implement B  -(void)callPrint{  [self.delegate print];  }  @end  

Then, implement print in Class:

@ Interface A: nsobject {B * B;}-(void) print; @ end @ implement a-(void) viewdidload {B = [[B alloc] init]; B. delegate = self; // set the proxy of B to self (a)}-(void) print {nslog (@ "print was called ");} // implement print @ end in

It is worth mentioning that:

The data source is almost the same as the delegate. The difference is that it is the same as the relationship between the delegate objects. The publish delegate object does not pass over the user interface control, but the data control to it. Objects that publish a delegate, usually view objects such as table views. It holds a data source reference and requests the data to be displayed from time to time. Data sources and delegates must follow a certain protocol and at least implement the methods required by the Protocol. The data source is responsible for managing the model object memory and providing it to the view of the release delegate.

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.