Objective-C學(xué)習(xí)筆記 利用協(xié)議實(shí)現(xiàn)回調(diào)函數(shù)
Objective-C學(xué)習(xí)筆記 利用協(xié)議實(shí)現(xiàn)回調(diào)函數(shù)是本文要介紹的內(nèi)容,主要是實(shí)現(xiàn)一個(gè)顯示文字為測(cè)試的視圖,然后經(jīng)過3秒鐘測(cè)試文字變?yōu)榛卣{(diào)函數(shù)文字。相應(yīng)的截圖如下:
實(shí)現(xiàn)的代碼如下:
定義協(xié)議:
- #import <UIKit/UIKit.h>
- @protocol NoteDelegate
- //回調(diào)函數(shù)
- -(void)messageCallBack:(NSString *)string;
- @end
調(diào)用協(xié)議:
- #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;
- //開始一個(gè)線程
- -(void)startThread
- {
- [NSTimer scheduledTimerWithTimeInterval:3
- target:self
- selector:@selector(targetMethod:)
- userInfo:nil
- repeats:NO];
- }
- -(void)targetMethod:(NSString *)string
- {
- if (self.noteDelegate!=nil) {
- //完成線程 調(diào)用回調(diào)函數(shù)
- [self.noteDelegate messageCallBack:@"回調(diào)函數(shù)"];
- }
- }
- @end
前臺(tái)頁(yè)面實(shí)現(xiàn):
- #import "IphoneDeleteViewController.h"
- #import "ManagerMessage.h"
- @implementation IphoneDeleteViewController
- @synthesize textView;
- //回調(diào)函數(shù)
- -(void)messageCallBack:(NSString *)string
- {
- self.textView.text=string;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.textView.text=@"測(cè)試";
- ManagerMessage *message=[[ManagerMessage alloc] init];
- //通知調(diào)用協(xié)議
- message.noteDelegate=self;
- [message startThread];
- [message release];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- self.textView=nil;
- }
- - (void)dealloc {
- [self.textView release];
- [super dealloc];
- }
- @end
小結(jié):Objective-C學(xué)習(xí)筆記 利用協(xié)議實(shí)現(xiàn)回調(diào)函數(shù)的內(nèi)容介紹完了,希望本文對(duì)你有所幫助。