最近對做IOS 項目遇到回調,抽空把相關資料整理下,以下是整理內容:
回調
回調就是將一段可執行的代碼和一個特定的事件綁定起來。當特定的事件發生時,就會執行這段代碼。
在Objective-C中,有四條途徑可以實現回調。
目標-動作對
在程序開始定等待前,要求“當時間發生時,向指定的對象發送某個特定的信息”。這裡接收消息的對象是目標,消息的選擇器是動作。
輔助對象
在程序開始等待之前,要求“當時間發生時,向遵守相應協議的輔助對象發送消息”。委托對象和數據源是常見的輔助對象。
通知
蘋果公司提供了一種稱為通知中心的對象。在程序開始等待前,就可以告知通知中心”某個對象正在等待某些特定的通知。當其中的某個通知出現時,向指定的對象發送特定的消息”。當事件發生時,相關的對象會向通知中心發布通知,然後再由通知中心將通知轉發給正在等待通知的對象。
Block對象
Block是一段可執行代碼。在程序開始等待前,聲明一個Block對象,當事件發生時,執行這段Block對象。
NSRunLoop
iOS中有一個NSRunLoop類,NSRunLoop實例會持續等待著,當特定的事件發生時,就會向相應的對象發送消息。NSRunLoop實例會在特定的事件發生時觸發回調。
循環
實現回調之前要先創建一個循環:
int main(int argc, const char * argv[]) {
@autoreleasepool {
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
目標-動作對
創建一個擁有NSRunLoop對象和NSTimer對象的應用程序。每隔兩秒,NSTimer對象會向其目標發送指定的動作消息,創建一個新的類,名為BNRLogger,為NSTimer對象的目標。
在BNRLogger.h中聲明動作方法:
#import <Foundation/Foundation.h> @interface BNRLogger : NSObject<NSURLSessionDataDelegate> @property(nonatomic) NSDate *lastTime; -(NSString *) lastTimeString; -(void)updateLastTime: (NSTimer *) t; @end
在BNRLogger.m中實現方法:
#import "BNRLogger.h"
@implementation BNRLogger
-(NSString *)lastTimeString
{
static NSDateFormatter *dateFormatter=nil;
if(!dateFormatter)
{
dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"created dateFormatter");
}
return [dateFormatter stringFromDate:self.lastTime];
}
-(void)updateLastTime:(NSTimer *)t
{
NSDate *now=[NSDate date];
[self setLastTime:now];
NSLog(@"Just set time to %@",self.lastTimeString);
}
@end
main.m中創建一個BNRLogger實例:
#import <Foundation/Foundation.h>
#import "BNRLogger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
BNRLogger *logger=[[BNRLogger alloc]init];
__unused NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(updateLastTime:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
輔助對象
我的上一篇Blog已經寫過NSURLSession方法的使用,那麼輔助對象回調的使用,將BNRLogger對象成為NSURLSession的委托對象,特定的事件發生時,該對象會向輔助對象發送消息。
main.m中創建一個NSURL對象以及NSURLRequest對象。然後創建一個NSURLSession對象,設置BNRLogger的實例為它的
委托對象:
#import <Foundation/Foundation.h>
#import "BNRLogger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
BNRLogger *logger=[[BNRLogger alloc]init];
//URL是一張圖片的下載鏈接
NSURL *url = [NSURL URLWithString:@"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg.xiazaizhijia.com%2Fuploads%2F2016%2F0914%2F20160914112151862.jpg&thumburl=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2349180720%2C2436282788%26fm%3D11%26gp%3D0.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
__unused NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:logger delegateQueue:[NSOperationQueue mainQueue]];
__unused NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(updateLastTime:) userInfo:nil repeats:YES];
//4.根據會話對象創建一個Task(發送請求)
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
//5.執行任務
[dataTask resume];
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
BNRLogger.h中,聲明NSURLSessionDataDelegate協議:
#import <Foundation/Foundation.h> @interface BNRLogger : NSObject<NSURLSessionDataDelegate> @property (nonatomic, strong) NSMutableData *responseData; @property(nonatomic) NSDate *lastTime; -(NSString *) lastTimeString; -(void)updateLastTime: (NSTimer *) t; @end
BNRLogger.m中,有NSURLSession的代理方法,具體可以看NSURLSession:
#import "BNRLogger.h"
@implementation BNRLogger
-(NSMutableData *)responseData
{
if (_responseData == nil) {
_responseData = [NSMutableData data];
}
return _responseData;
}
-(NSString *)lastTimeString
{
static NSDateFormatter *dateFormatter=nil;
if(!dateFormatter)
{
dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"created dateFormatter");
}
return [dateFormatter stringFromDate:self.lastTime];
}
-(void)updateLastTime:(NSTimer *)t
{
NSDate *now=[NSDate date];
[self setLastTime:now];
NSLog(@"Just set time to %@",self.lastTimeString);
}
//1.接收到服務器響應的時候調用該方法
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
//在該方法中可以得到響應頭信息,即response
NSLog(@"didReceiveResponse--%@",[NSThread currentThread]);
NSLog(@"響應");
//注意:需要使用completionHandler回調告訴系統應該如何處理服務器返回的數據
//默認是取消的
/*
NSURLSessionResponseCancel = 0, 默認的處理方式,取消
NSURLSessionResponseAllow = 1, 接收服務器返回的數據
NSURLSessionResponseBecomeDownload = 2,變成一個下載請求
NSURLSessionResponseBecomeStream 變成一個流
*/
completionHandler(NSURLSessionResponseAllow);
}
//2.接收到服務器返回數據的時候會調用該方法,如果數據較大那麼該方法可能會調用多次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData--%@",[NSThread currentThread]);
NSLog(@"返回");
//拼接服務器返回的數據
[self.responseData appendData:data];
}
//3.當請求完成(成功|失敗)的時候會調用該方法,如果請求失敗,則error有值
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"didCompleteWithError--%@",[NSThread currentThread]);
NSLog(@"完成");
if(error == nil)
{
//解析數據
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];
NSLog(@"%@",dict);
}
}
@end
通知
當系統時區發生變化時,會向通知中心發布NSSystemTimeZoneDidChangeNotification通知,然後通知中心會將該通知轉發給相應的觀察者。
main.m中將BNRLogger實例注冊為觀察者,系統時區設置發生變化可以收到相應的通知:
//在”輔助對象”方法應用程序中的main.m中加入這行代碼 [[NSNotificationCenter defaultCenter]addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
在BNRLogger.m中實現該方法:
//在”輔助對象”方法應用程序中的BNRLogger.m中加入這行代碼
-(void)zoneChange:(NSNotification *)note
{
NSLog(@"The system time zone has changed!");
}
Block回調
把上面所講的“通知”方法應用程序main.m中的:
[[NSNotificationCenter defaultCenter]addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
改為:
[[NSNotificationCenter defaultCenter]addObserverForName:NSSystemTimeZoneDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
NSLog(@"The system time zone has changed!");
}];
“通知”方法應用程序BNRLogger.m中的這個方法去掉:
-(void)zoneChange:(NSNotification *)note
{
NSLog(@"The system time zone has changed!");
}
總結
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!