iOS 四種回調方法總結
最近對做IOS 項目遇到回調,抽空把相關資料整理下,以下是整理內容:
回調
回調就是將一段可執(zhí)行的代碼和一個特定的事件綁定起來。當特定的事件發(fā)生時,就會執(zhí)行這段代碼。
在Objective-C中,有四條途徑可以實現(xiàn)回調。
目標-動作對
在程序開始定等待前,要求“當時間發(fā)生時,向指定的對象發(fā)送某個特定的信息”。這里接收消息的對象是目標,消息的選擇器是動作。
輔助對象
在程序開始等待之前,要求“當時間發(fā)生時,向遵守相應協(xié)議的輔助對象發(fā)送消息”。委托對象和數(shù)據(jù)源是常見的輔助對象。
通知
蘋果公司提供了一種稱為通知中心的對象。在程序開始等待前,就可以告知通知中心”某個對象正在等待某些特定的通知。當其中的某個通知出現(xiàn)時,向指定的對象發(fā)送特定的消息”。當事件發(fā)生時,相關的對象會向通知中心發(fā)布通知,然后再由通知中心將通知轉發(fā)給正在等待通知的對象。
Block對象
Block是一段可執(zhí)行代碼。在程序開始等待前,聲明一個Block對象,當事件發(fā)生時,執(zhí)行這段Block對象。
NSRunLoop
iOS中有一個NSRunLoop類,NSRunLoop實例會持續(xù)等待著,當特定的事件發(fā)生時,就會向相應的對象發(fā)送消息。NSRunLoop實例會在特定的事件發(fā)生時觸發(fā)回調。
循環(huán)
實現(xiàn)回調之前要先創(chuàng)建一個循環(huán):
int main(int argc, const char * argv[]) {
@autoreleasepool {
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
目標-動作對
創(chuàng)建一個擁有NSRunLoop對象和NSTimer對象的應用程序。每隔兩秒,NSTimer對象會向其目標發(fā)送指定的動作消息,創(chuàng)建一個新的類,名為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中實現(xiàn)方法:
#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中創(chuàng)建一個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已經(jīng)寫過NSURLSession方法的使用,那么輔助對象回調的使用,將BNRLogger對象成為NSURLSession的委托對象,特定的事件發(fā)生時,該對象會向輔助對象發(fā)送消息。
main.m中創(chuàng)建一個NSURL對象以及NSURLRequest對象。然后創(chuàng)建一個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.根據(jù)會話對象創(chuàng)建一個Task(發(fā)送請求)
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
//5.執(zhí)行任務
[dataTask resume];
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
BNRLogger.h中,聲明NSURLSessionDataDelegate協(xié)議:
#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回調告訴系統(tǒng)應該如何處理服務器返回的數(shù)據(jù)
//默認是取消的
/*
NSURLSessionResponseCancel = 0, 默認的處理方式,取消
NSURLSessionResponseAllow = 1, 接收服務器返回的數(shù)據(jù)
NSURLSessionResponseBecomeDownload = 2,變成一個下載請求
NSURLSessionResponseBecomeStream 變成一個流
*/
completionHandler(NSURLSessionResponseAllow);
}
//2.接收到服務器返回數(shù)據(jù)的時候會調用該方法,如果數(shù)據(jù)較大那么該方法可能會調用多次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData--%@",[NSThread currentThread]);
NSLog(@"返回");
//拼接服務器返回的數(shù)據(jù)
[self.responseData appendData:data];
}
//3.當請求完成(成功|失敗)的時候會調用該方法,如果請求失敗,則error有值
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"didCompleteWithError--%@",[NSThread currentThread]);
NSLog(@"完成");
if(error == nil)
{
//解析數(shù)據(jù)
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];
NSLog(@"%@",dict);
}
}
@end
通知
當系統(tǒng)時區(qū)發(fā)生變化時,會向通知中心發(fā)布NSSystemTimeZoneDidChangeNotification通知,然后通知中心會將該通知轉發(fā)給相應的觀察者。
main.m中將BNRLogger實例注冊為觀察者,系統(tǒng)時區(qū)設置發(fā)生變化可以收到相應的通知:
//在”輔助對象”方法應用程序中的main.m中加入這行代碼 [[NSNotificationCenter defaultCenter]addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
在BNRLogger.m中實現(xiàn)該方法:
//在”輔助對象”方法應用程序中的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!");
}
總結
- 對于只做一件事情的對象(例如),使用目標-動作對。
- 對于功能更復雜的對象(例如NSURLSession),使用輔助對象。最常見的輔助對象類型是委托對象。
- 對于要觸發(fā)多個(其他對象中的)回調的對象(例如NSTimeZone),使用通知。
- Block實現(xiàn)回調使代碼便于閱讀。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
IOS 下獲取 rootviewcontroller 的版本不同的問題解決辦法
這篇文章主要介紹了IOS 下獲取 rootviewcontroller 的版本不同的問題解決辦法的相關資料,希望通過本文能幫助到大家,讓大家遇到這種問題可以解決,需要的朋友可以參考下2017-10-10
iOS 頁面滑動與標題切換顏色漸變的聯(lián)動效果實例
本篇文章主要介紹了iOS 頁面滑動與標題切換顏色漸變的聯(lián)動效果實例,具有一定的參考價值,有興趣的可以了解一下。2017-04-04

