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:
- # Import <UIKit/UIKit. h>
- @ Protocol NoteDelegate
- // Callback function
- -(Void) messageCallBack :( NSString *) string;
- @ End
Call Protocol:
- # Import <Foundation/Foundation. h>
- # Import "NoteDelegate. h"
- @ Interface ManagerMessage: NSObject {
- Id <NoteDelegate> * noteDelegate;
- }
- @ Property (nonatomic, retain) id <NoteDelegate> * noteDelegate;
- -(Void) startThread;
- @ End
-
- # Import "ManagerMessage. h"
- @ Implementation ManagerMessage
- @ Synthesize noteDelegate;
- // Start a thread
- -(Void) startThread
- {
-
- [NSTimer scheduledTimerWithTimeInterval: 3
- Target: self
- Selector: @ selector (targetMethod :)
- UserInfo: nil
- Repeats: NO];
- }
- -(Void) targetMethod :( NSString *) string
- {
- If (self. noteDelegate! = Nil ){
- // Complete the callback function called by the thread
- [Self. noteDelegate messageCallBack: @ "callback function"];
- }
- }
- @ End
Foreground page implementation:
- # Import "IphoneDeleteViewController. h"
- # Import "ManagerMessage. h"
- @ Implementation IphoneDeleteViewController
- @ Synthesize textView;
-
- // Callback function
- -(Void) messageCallBack :( NSString *) string
- {
- Self. textView. text = string;
- }
- -(Void) viewDidLoad {
- [Super viewDidLoad];
- Self. textView. text = @ "test ";
- ManagerMessage * message = [[ManagerMessage alloc] init];
- // Notification Call Protocol
- Message. noteDelegate = self;
- [Message startThread];
- [Message release];
- }
- -(Void) didReceiveMemoryWarning {
- [Super didReceiveMemoryWarning];
- }
- -(Void) viewDidUnload {
- Self. textView = nil;
- }
- -(Void) dealloc {
- [Self. textView release];
- [Super dealloc];
- }
- @ 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.