[iOS]Objective-C利用协议实现回调函数(类似java的回调函数)

在编写iphone开发时,最常用的就是回调函数。自己编写回调函数,实现动态加载数据,加载完数据之后就利用回调函数通知给前台页面,显示相应数据的界面。在iphone中利用协议可以很容易的实现回调函数,后台加载数据,然后显示在前台页面.

 

下面举个通俗的例子:
    某天,我打电话向你请教问题,当然是个难题,疑问,你一时想不出解决方法,我又不能拿着电话在那里傻等,于是我们约定:等你想出办法后打电话通知我,这样,我就挂掉电话办其它事情去了。过了XX分钟,我的手机响了,你兴高采烈的说问题已经搞定,应该如此这般处理。故事到此结束。
这个例子说明了“异步+回调”的编程模式其中,你后来打电话告诉我结果便是一个“回调”过程;我的手机号码必须在以前告诉你,这便是注册回调函数;我的手机号码应该有效并且手机能够接收到你的呼叫,这是回调函数必须符合接口规范。


 
通过上面个人感觉到回调更多的应用就是结合异步。比如:Ajax中js通过组件和服务器的异步通信。



Objective-C利用协议实现回调函数(类似java的回调函数)
在编写iphone开发时,最常用的就是回调函数。自己编写回调函数,实现动态加载数据,加载完数据之后就利用回调函数通知给前台页面,显示相应数据的界面。在iphone中利用协议可以很容易的实现回调函数,后台加载数据,然后显示在前台页面。

 

 

Protocol协议的用法 

protocol 
一、说明
 
两个类进行通讯,用协议就比较方便。 


(书本上的东东,还是得看看) 
1.协议声明了可以被任何类实现的方法 
2.协议不是类,它是定义了一个其他对象可以实现的接口 
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。 
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。 
5:委托,就是调用自己定义方法,别的类来实现。 
6.新特性说明 
@optional预编译指令:表示可以选择实现的方法 
@required预编译指令:表示必须强制实现的方法 


二、定义 


.h 
@protocol ContactCtrlDelegate 
-(void)DismissContactsCtrl; 
@end 


@interface ContactsCtrl : UIViewController { 
    id <ContactCtrlDelegate> delegate; 
} 
@property (nonatomic, assign) id <ContactCtrlDelegate> delegate; 
.m 
@synthesize delegate; 




三、例子 


例如:UITextView 
@protocol UITextViewDelegate <NSObject> 


@optional 


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView; 
- (BOOL)textViewShouldEndEditing:(UITextView *)textView; 
- (void)textViewDidBeginEditing:(UITextView *)textView; 
- (void)textViewDidEndEditing:(UITextView *)textView; 
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; 
- (void)textViewDidChange:(UITextView *)textView; 
- (void)textViewDidChangeSelection:(UITextView *)textView; 

@end 


如果要调用以上这些方法,就必须设置UITextView的委托:TextView.delegate = self; 




四、Demo 
1、ContactsCtrl.h 


#import <UIKit/UIKit.h>


//定义协议 

@protocol ContactCtrlDelegate 


-(void)DismissContactsCtrl;//默认@required


@end 


@interface ContactsCtrl : UIViewController

    IBOutlet UINavigationBar *ContactNavBar; 

    id <ContactCtrlDelegate> delegate;//就是一个类指针,指向实现它的类,ContactsCtrl.delegate = xxx.(xxxContactCtrlDelegate的实现类)

@property (nonatomic, assign) id <ContactCtrlDelegate> delegate; 

-(IBAction)canCelBtn:(id)sender; 

@end


2、ContactsCtrl.m  


#import "ContactsCtrl.h"


@implementation ContactsCtrl 

@synthesize delegate;


//调用协议中的方法 

-(IBAction)canCelBtn:(id)sender{ 

    [delegate DismissContactsCtrl]; 

//    [self dismissModalViewControllerAnimated:YES];

}


@end


3、ProtocolDemoCtrl.h  

#import <UIKit/UIKit.h> 

#import "ContactsCtrl.h" 

@interface ProtocolDemoCtrl : UIViewController <ContactCtrlDelegate>{//添加委托 

    NSString *string;

- (IBAction)addContactAction:(id)sender;


@end


4、ProtocolDemoCtrl.m  


#import "ProtocolDemoCtrl.h" 



@implementation ProtocolDemoCtrl 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 

- (void)viewDidLoad { 

    [super viewDidLoad];

    string = [[NSString alloc] initWithFormat:@"ProtocolDemoCtrl"];



- (void)addContactAction:(id)sender{ 

    ContactsCtrl *contactsView = [[ContactsCtrl alloc] initWithNibName:@"ContactsCtrl" bundle:nil];

    contactsView.delegate = self;//设置委托 

    [self presentModalViewController:contactsView animated:YES]; 

    [contactsView release];     



//实现ContactCtrlDelegate协议中的方法 

-(void)DismissContactsCtrl{ 

    NSLog(@"%@",string);

    [self dismissModalViewControllerAnimated:YES]; 


-(void)dealloc {

    [string release];

    

    [super dealloc];

}

@end


 

网络的回调函数, 如果你指的是- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
那么只有在全部数据接收完后才会被系统调用
如果你想收到一点就处理一点的话
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
这个回调能满足你的要求
总之, 你先弄明白什么叫回调函数吧。才能学会调接口,从服务器上取数据,显示到手机界面上。(非常重要的基础语法知识)
所谓回调函数,是指callback,请先理解。
回调函数的理解
所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数。
例如Win32下的窗口过程函数就是一个典型的回调函数。一般说来,C不会自己调用B,C提供B的目的就是让S来调用它,而且是C不得不提供。
由于S并不知道C提供的B姓甚名谁,所以S会约定B的接口规范(函数原型),然后由C提前通过S的一个函数R告诉S自己将要使用B函数,
这个过程称为回调函数的注册,R称为注册函数。WebService以及Java的RMI都用到回调机制,可以访问远程服务器程序。
其实回调函数并不神秘
对于很多初学者来说,往往觉得回调函数很神秘,很想知道回调函数的工作原理。

 

 


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值