Objective-C learning Notes use protocols to implement callback Functions

Source: Internet
Author: User

Objective-CLearning notes UtilizationProtocolImplement callbackFunctionIs the content to be introduced in this article, mainly to implement a display text as a test view, and then after 3 seconds to test the text into a callbackFunctionText. The response is as follows:

 

The implementation code is as follows:

Definition protocol:

 
 
  1. # Import <UIKit/UIKit. h>
  2. @ Protocol NoteDelegate
  3. // Callback function
  4. -(Void) messageCallBack :( NSString *) string;
  5. @ End

Call Protocol:

 
 
  1. # Import <Foundation/Foundation. h>
  2. # Import "NoteDelegate. h"
  3. @ Interface ManagerMessage: NSObject {
  4. Id <NoteDelegate> * noteDelegate;
  5. }
  6. @ Property (nonatomic, retain) id <NoteDelegate> * noteDelegate;
  7. -(Void) startThread;
  8. @ End
  9.  
  10. # Import "ManagerMessage. h"
  11. @ Implementation ManagerMessage
  12. @ Synthesize noteDelegate;
  13. // Start a thread
  14. -(Void) startThread
  15. {
  16.  
  17. [NSTimer scheduledTimerWithTimeInterval: 3
  18. Target: self
  19. Selector: @ selector (targetMethod :)
  20. UserInfo: nil
  21. Repeats: NO];
  22. }
  23. -(Void) targetMethod :( NSString *) string
  24. {
  25. If (self. noteDelegate! = Nil ){
  26. // Complete the callback function called by the thread
  27. [Self. noteDelegate messageCallBack: @ "callback function"];
  28. }
  29. }
  30. @ End

Foreground page implementation:

 
 
  1. # Import "IphoneDeleteViewController. h"
  2. # Import "ManagerMessage. h"
  3. @ Implementation IphoneDeleteViewController
  4. @ Synthesize textView;
  5.  
  6. // Callback function
  7. -(Void) messageCallBack :( NSString *) string
  8. {
  9. Self. textView. text = string;
  10. }
  11. -(Void) viewDidLoad {
  12. [Super viewDidLoad];
  13. Self. textView. text = @ "test ";
  14. ManagerMessage * message = [[ManagerMessage alloc] init];
  15. // Notification Call Protocol
  16. Message. noteDelegate = self;
  17. [Message startThread];
  18. [Message release];
  19. }
  20. -(Void) didReceiveMemoryWarning {
  21. [Super didReceiveMemoryWarning];
  22. }
  23. -(Void) viewDidUnload {
  24. Self. textView = nil;
  25. }
  26. -(Void) dealloc {
  27. [Self. textView release];
  28. [Super dealloc];
  29. }
  30. @ End

Summary: The introduction of Objective-C learning notes using the Protocol to implement callback functions is complete. I 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.