Detailed objective-c in the Commission and agreement

Source: Internet
Author: User

Objective-c delegation and protocol this has no relationship, the agreement as mentioned above, is to play the role of pure virtual class in C + +, for the "delegation" is not related to the agreement, but we often use the Protocol also implement the mechanism of the delegation, in fact, without agreement can also be fully implemented.

AD: "Active" web and app compatibility Combat WIN10 training Camp Free Registration

In objective-c , the delegation and protocol are the contents of this article, and the principal and protocol are two concepts, and the protocol is actually equivalent to the concept of pure virtual class in C + +. Only defined and implemented by other classes only. The delegate is similar to an interface in Java. (objective-c Implementation of this mechanism is implemented by agreement, which I now think is wrong, for the following reasons:).

Objective-c delegation and protocol this has no relationship, the agreement as mentioned above, is to play the role of pure virtual class in C + +, for the "delegation" is not related to the agreement, but we often use the Protocol also implement the mechanism of the delegation, in fact, without agreement can also be fully implemented. Here's how it's implemented:

Define a Class A:

    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 the instance of Class A as a member variable in B:

    1. @interface b:nsobject{
    2. A *a_delegate;
    3. }
    4. @end

The following is the implementation of the delegation 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];
    7. }

In this way, the most basic mechanism is completed, to apply the most popular sentence explanation: B needs to complete a print operation, but he did not implement this operation, but rather to a to complete, and only when needed to invoke a in the implementation of the print operation.

Here's another way to implement this approach, which is closer to what we usually see in terms of protocol implementations:

We still define a Class A:

    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

The definition of Class B is then changed to the following:

    1. @interface b:nsobject{
    2. ID Delegate
    3. }
    4. @propert (nonamtic,retain) ID delegate;
    5. @end
    6. Now we don't use the main () function to implement the delegate mechanism in the implementation part of B:
    7. @implement B
    8. -(void) callprint{
    9. [Self.delegate print];
    10. }
    11. @end

The above implementation is the same as the first, but the first is the invocation of a delegate method in a third-party function. Delegate is an ID type, in this case an instance of Class A, which can of course call print in Class A. The second method does not have a third-party function, which is to invoke the method in Class A in class B. In other words, B requires the Print method, does not implement itself, let a to implement, call itself.

Next is the most common way to implement a delegate with a protocol, as described below:

protocol-Agreement, is the use of this agreement will follow this agreement to do, the Protocol requires the implementation of the method must be realized.

Delegate-Entrust, as the name implies is entrusted to others, is when a thing happened, not to deal with, let others to deal with.

When a view contains a B view

b View needs to modify the A view interface, then this time you need to use the delegate.

Requires several steps

1, first set a protocol

2. A view implementation method in the Protocol

3, B View Set a delegate variable

4, the B view of the delegate variable is set to a view, meaning, B view commissioned a view to do things.

5. When the event occurs, the protocol method in a view is invoked with the delegate variable

Example:

  1. B_view.h:
  2. @protocol uibviewdelegate <nsobject>
  3. @optional
  4. -(void) Ontouch: (Uiscrollview *) ScrollView; Declaring protocol methods
  5. @end
  6. @interface Bview:uiscrollview<uiscrollviewdelegate>
  7. {
  8. ID< uibviewdelegate > _touchdelegate;//Set delegate variable
  9. }
  10. @property (nonatomic,assign) ID< uibviewdelegate > _touchdelegate;
  11. @end
  12. B_VIEW.MM:
  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]; Invoking the Protocol delegate
  26. }
  27. @end
  28. A_view.h:
  29. @interface Aviewcontroller:uiviewcontroller < uibviewdelegate >
  30. {
  31. Bview *m_bview;
  32. }
  33. @end
  34. A_VIEW.MM:
  35. -(void) Viewwillappear: (BOOL) animated
  36. {
  37. M_bview._touchdelegate = self ;//Set Delegate
  38. [Self.view Addsubview:m_bview];
  39. }
  40. -(void) Ontouch: (Uiscrollview *) ScrollView
  41. {
  42. Implementation protocol
  43. }

Summary: Detailed objective-c of the contents of the Commission and the agreement , hope that through this study can help you!

Detailed objective-c in the Commission and agreement

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.