Objective-C利用协议实现回调函数

Objective-C利用协议实现回调函数

实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:

 

image image

实现的代码如下:

定义协议:

#import <UIKit/UIKit.h> 
@protocol NoteDelegate 
//回调函数 
-(void)messageCallBack:(NSString *)string; 
@end

 

调用协议:

#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; 
//开始一个线程 
-(void)startThread 
{

    [NSTimer scheduledTimerWithTimeInterval:3 
                                     target:self 
                                   selector:@selector(targetMethod:) 
                                   userInfo:nil 
                                    repeats:NO]; 

-(void)targetMethod:(NSString *)string 

    if (self.noteDelegate!=nil) { 
        //完成线程 调用回调函数 
        [self.noteDelegate messageCallBack:@"回调函数"]; 
        } 

@end

前台页面实现:

#import "IphoneDeleteViewController.h" 
#import "ManagerMessage.h" 
@implementation IphoneDeleteViewController 
@synthesize textView;

//回调函数 
-(void)messageCallBack:(NSString *)string 

    self.textView.text=string; 
}

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.textView.text=@"测试"; 
    ManagerMessage *message=[[ManagerMessage alloc] init]; 
    //通知调用协议 
    message.noteDelegate=self; 
    [message startThread]; 
    [message release]; 
}

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
}

- (void)viewDidUnload { 
    self.textView=nil; 
}

- (void)dealloc { 
    [self.textView release]; 
    [super dealloc]; 
}

@end

实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:

 

image image

实现的代码如下:

定义协议:

#import <UIKit/UIKit.h> 
@protocol NoteDelegate 
//回调函数 
-(void)messageCallBack:(NSString *)string; 
@end

 

调用协议:

#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; 
//开始一个线程 
-(void)startThread 
{

    [NSTimer scheduledTimerWithTimeInterval:3 
                                     target:self 
                                   selector:@selector(targetMethod:) 
                                   userInfo:nil 
                                    repeats:NO]; 

-(void)targetMethod:(NSString *)string 

    if (self.noteDelegate!=nil) { 
        //完成线程 调用回调函数 
        [self.noteDelegate messageCallBack:@"回调函数"]; 
        } 

@end

前台页面实现:

#import "IphoneDeleteViewController.h" 
#import "ManagerMessage.h" 
@implementation IphoneDeleteViewController 
@synthesize textView;

//回调函数 
-(void)messageCallBack:(NSString *)string 

    self.textView.text=string; 
}

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.textView.text=@"测试"; 
    ManagerMessage *message=[[ManagerMessage alloc] init]; 
    //通知调用协议 
    message.noteDelegate=self; 
    [message startThread]; 
    [message release]; 
}

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
}

- (void)viewDidUnload { 
    self.textView=nil; 
}

- (void)dealloc { 
    [self.textView release]; 
    [super dealloc]; 
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值