Objective-C protocol and Delegation

Source: Internet
Author: User

Objective-C ProtocolAndDelegateThis is the content to be introduced in this article.Objective-CMediumProtocolAndDelegateThrough examples, we can learn more quickly and conveniently.Objective-CFirst, let's look at the details.

Protocol-ProtocolThat is, after using this agreement, we must follow this agreement, and the methods required by the Agreement must be implemented.

Delegate-DelegateAs the name suggests, it is to entrust others to do things, that is, when one thing happens, they will not handle it themselves and let others handle it.

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.

Example:

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

Summary:Objective-C ProtocolAndDelegateI hope this article 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.