Objective-C学习笔记 利用协议实现回调函数

移动开发 iOS
本文介绍的是Objective-C学习笔记 利用协议实现回调函数,主要是以一个小实例来实现函数的回调,我们来看内容。

Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:

[[37472]] 

[[37473]]

实现的代码如下:

定义协议:

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

调用协议:

  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. //开始一个线程   
  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.         //完成线程 调用回调函数   
  27.         [self.noteDelegate messageCallBack:@"回调函数"];   
  28.         }   
  29. }   
  30. @end 

前台页面实现:

  1. #import "IphoneDeleteViewController.h"   
  2. #import "ManagerMessage.h"   
  3. @implementation IphoneDeleteViewController   
  4. @synthesize textView;   
  5.  
  6. //回调函数   
  7. -(void)messageCallBack:(NSString *)string   
  8. {   
  9.     self.textView.text=string;   
  10. }   
  11. - (void)viewDidLoad {   
  12.     [super viewDidLoad];   
  13.     self.textView.text=@"测试";   
  14.     ManagerMessage *message=[[ManagerMessage alloc] init];   
  15.     //通知调用协议   
  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 

小结:Objective-C学习笔记 利用协议实现回调函数的内容介绍完了,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-08-05 14:03:39

Objective-C 对象 模板

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2013-06-20 10:40:32

Objective-C实现截图

2011-07-22 15:42:39

Objective-C UIView 内存

2011-08-17 10:58:59

Objective-C构造函数

2013-04-11 13:41:30

Objective-CiOS编程

2011-08-15 16:09:44

Cocoa对象Objective-C

2011-07-27 16:36:03

iphone Objective- 静态库

2011-08-03 15:51:48

Objective-C 协议 委托

2011-08-04 11:15:46

Objective-C 构造函数 构造方法

2011-08-04 10:38:17

Objective-C 预处理程序

2011-05-11 14:06:49

Objective-C

2013-08-21 14:57:42

objective-c问题

2011-08-15 17:29:36

Objective-C构造函数

2011-07-27 16:18:42

Objective-c 协议

2011-08-04 11:04:14

Objective-C 面向对象 继承

2011-08-22 15:31:35

Objective-C协议

2011-08-15 14:32:42

Objective-C委托协议

2011-07-25 10:03:06

Objective-C 委托
点赞
收藏

51CTO技术栈公众号