在我们日常的开发任务中,我们经常要用到一些回调的手段,譬如说网络请求操作,网络请求过程一般都是比较耗时的,在程序设计中我们都会采用异步操作来取代同步操作;还有就是代码中主要的业务逻辑,我们在开发中需要获取相应的数据进行加工这也会用到回调,等等方面表明回调在我们开发中占据着很重要的地位。
好滴~讲了一下回调经常出现的场景,那我们就正式进入主题,谈谈IOS开发中我常用的几种回调手段。
1.NotificationCenter 通知中心
消息通知机制顾名思义,在IOS开发中它就是通过消息,来达到通知的目的。我们需要在通知中心注册我们想要监听的消息,当项目中有地方发出这个消息的时候,通知中心会发送给注册这个消息的对象。
a. addObserver
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMessageContent:) name:@"Notification_Send_Message" object:nil];
}
b.postNotification
- (void)sendTheMessage:(NSMutableDictionary *)m_dic{
// [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_Send_Message" object:m_dic];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_Send_Message" object:m_dic userInfo:nil];
}
c.处理消息
- (void)getMessageContent:(NSNotification *)notifi{
NSMutableArray *m_array = (NSMutableArray *)notifi.object;
NSLog(@"++++++%@++++++", m_array);
//........
}
d.removeObserver
- (void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"Notification_Send_Message" object:nil];
}
2.block(闭包)
Block是一种比较特殊的数据类型,它可以用于两个界面质检传值,也可以对代码封装作为参数传递。block常常结合typedef来使用,用自己定义的类型去创建block显得更加的简单便捷,接下来举例实现一个block回调,将传入的参数加上另一个值后再回调回来:
SecondViewController的实现:
#import <UIKit/UIKit.h>
//自定义一个block
typedef void(^changeValueBlock)(int value);
@interface SecondViewController : UIViewController
@property (strong, nonatomic) changeValueBlock changeBlock;
@property (assign) int oldValue;
- (void)updateTheValue:(int) oldValue Block:(changeValueBlock)block;
- (IBAction)clickTheBtn:(id)sender;
@end
FirstviewController的实现:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.secondView updateTheValue:10 Block:^(int value) {
//回调
NSLog(@"received the new value:%d", value);
}];
}
3.delegate 委托
委托其实是一种设计模式,通俗一点来讲就是当自己有需求要处理但是不方便的时候,就建立一个委托,请别人来帮忙处理。举个例子:“我要给路人甲打电话,但是我不知道李斯的电话号码;我就拜托章三去查询,章三查到后就发短信给了我号码”,章三就是我的委托对象。相信大家在ios开发中经常会看到类似@protocol(协议)的代码吧!如果我们要实现一个delegate委托,就先要先定义protocol(协议),在指定收到回调的类中(也就是我)去实现协议中的函数(例如收短信),如果没有实现,编译器就会报警告;下面是一个简单的例子,SecondviewController会回调FirstViewController,FirstViewController实现协议中的回调函数:
协议:ViewSelectedDelegate
#ifndef ViewSelectedDelegate_h
#define ViewSelectedDelegate_h
@protocol ViewSelectedDelegate <NSObject>
- (void)viewSelectedAtIndex:(int)index;
@end
#endif /* ViewSelectedDelegate_h */
FirstViewController:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.secondView.m_delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewSelectedAtIndex:(int)index{
NSLog(@"select view at index %d", index);
}
SecondViewController:
@interface SecondViewController : UIViewController
@property (strong, nonatomic) id<ViewSelectedDelegate> m_delegate;
- (IBAction)clickTheBtn:(id)sender;
@implementation SecondViewController
@synthesize m_delegate = _m_delegate;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clickTheBtn:(id)sender {
[self.m_delegate viewSelectedAtIndex:1];
}
总结:这三种回调都是ios开发中常见的回调方法,大家在开发的过程中可以依据情况使用。
大家可以关注我的微信公众号与我互动,相关问题也可以直接用公众号联系我: