Details about delegation and protocols in Objective-C

Source: Internet
Author: User

Objective-CMediumDelegateAndProtocolIs the content to be introduced in this article,DelegateAndProtocolThere are two concepts. The protocol is actually equivalent to the pure virtual class concept in C ++. It can only be defined and implemented by other classes. Delegation is similar to interfaces in Java.Objective-CThe mechanism for implementing delegation is implemented using protocols. I think this is not true for the following reasons :).

The Objective-C delegation does not have any relationship with the Protocol. As described above, the Protocol serves as a pure virtual class in C ++, and does not have any relationship with the protocol for "delegation, however, we often use the Protocol to implement the delegation mechanism. In fact, we can implement delegation without the protocol. The implementation method is as follows:

Define A Class:

  1. @interface A:NSObject  
  2. -(void)print;  
  3. @end  
  4. @implement A  
  5. -(void)print{  
  6. }  
  7. @end 

Define A class B. In B, define an instance of Class A as A member variable in Class B:

  1. @interface B:NSObject{  
  2.        A *a_delegate;  
  3. }  
  4. @end 

The following describes how to implement the delegate mechanism in the mai () function:

  1. void main()  
  2. {  
  3. B *b=[[B alloc]init];  
  4. A *a=[[A alloc]init];  
  5. b.a_delegate=a;  
  6. [b.a_delegate print];  

In this way, the most basic delegation mechanism is complete. To apply the most common explanation, B needs to complete a print operation, but he does not implement this operation himself, instead, it is handed over to A to complete the operation. You only need to call the print operation implemented in A as needed.

Next we will write another implementation method, which is closer to the Protocol implementation method we usually see:

We still define A Class:

  1. @interface A:NSObject{  
  2. B *b;  
  3. }  
  4. -(void)print;  
  5. @end  
  6. @implement A  
  7. @synasize delegate;  
  8. -(void)viewDidLoad{  
  9. b=[[B alloc]init];  
  10. b.delegate=self;  
  11. }  
  12. -(void)print{  
  13. NSLog(@"print was called");  
  14. }  
  15. @end 

Then change the definition of Class B to the following:

  1. @ Interface B: NSObject {
  2. Id delegate
  3. }
  4. @ Propert (nonamtic, retain) id delegate;
  5. @ End
  7. Now we don't need the main () function to implement the delegate mechanism in the implementation part of B:
  9. @ Implement B
  10. -(Void) callPrint {
  11. [Self. delegate print];
  12. }
  13. @ End

The above implementation method is the same as the first one, but the first is to call the delegate method in a third-party function. Delegate is an id type. In this example, it is an instance of Class A. Of course, you can call print in Class. The second method does not include third-party functions. It is to call methods in Class A in Class B. In other words, the print method is required in B, which is not implemented by yourself. Let A implement it and call it by yourself.

Next, we will discuss the most common way to implement delegation using protocols, as described below:

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.

When A view contains B view

B view needs to modify the view interface, so delegation is required at this time.

Several steps are required

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 a view, which means that B view delegates a view to do things.

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


  1. B _View.h:
  2. @ Protocol UIBViewDelegate <NSObject>
  3. @ Optional
  4. -(Void) ontouch :( UIScrollView *) scrollView; // declares the Protocol method.
  5. @ End
  6. @ Interface BView: UIScrollView <UIScrollViewDelegate>
  7. {
  8. Id <UIBViewDelegate> _ touchdelegate; // sets the delegate variable.
  9. }
  10. @ Property (nonatomic, assign) id <UIBViewDelegate> _ touchdelegate;
  11. @ End
  12. B
  13. @ Synthesize _ touchdelegate;
  14. -(Id) initWithFrame :( CGRect) frame {
  15. If (self = [super initWithFrame: frame]) {
  16. // Initialization code
  17. _ Touchdelegate = nil;
  18. }
  19. Return self;
  20. }
  21. -(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
  22. {
  23. [Super touchesBegan: touches withEvent: event];
  24. If (_ touchdelegate! = Nil & [_ touchdelegate respondsToSelector: @ selector (ontouch :)] = true)
  25. [_ Touchdelegate ontouch: self]; // call protocol delegate
  26. }
  28. @ End
  29. A_View.h:
  30. @ Interface AViewController: UIViewController <UIBViewDelegate>
  31. {
  32. BView * m_BView;
  33. }
  34. @ End
  36. -(Void) viewWillAppear :( BOOL) animated
  37. {
  38. M_BView. _ touchdelegate = self; // sets the delegate
  39. [Self. view addSubview: m_BView];
  40. }
  41. -(Void) ontouch :( UIScrollView *) scrollView
  42. {
  43. // Implementation Protocol
  44. }

Summary: DetailsObjective-CMediumDelegateAndProtocolI hope this article will help you with the introduction!

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: 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.