如何执行委托代替通知 [英] How to implement delegate in place of notification

查看:134
本文介绍了如何执行委托代替通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

## NetworkClass

## NetworkClass

-(void)getResponse:(NSString *)url{

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [urlRequest setHTTPMethod:@"GET"];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

    NSURLSessionDataTask *task = [session dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //check if we encountered an error
        if(error != nil){
            NSLog(@"%@", [error localizedDescription]);
        }else{
            //get and check the HTTP status code
            NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
            if (HTTPStatusCode != 200) {
                NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode);
            }

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                if(data != nil){
                    NSError *parseError = nil;
                    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadNotification"
                                                                        object:self
                                                                      userInfo:responseDictionary];
                    NSLog(@"The response is - %@",responseDictionary);


                }
            }];
        }
    }];


    [task resume];

}



ViewController



ViewController

-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyReload:) name:@"ReloadNotification" object:nil];
}

这里我已经传达给viewcontroller,响应来自服务器,并且反映通过使用NSNOTIFICATION对视图控制器的响应我实际上想通过代理实现相同的事情。我是一个新的程序员,试图学习代表,但不能理解这些概念,请用代码解释如何完成同样的任务通过代表提前谢谢!

Here I have communicated to the viewcontroller that response has come from server and kindly reflect the response on view controller by using NSNOTIFICATION .I actually want to implement the same thing through delegates .I am a new programmer and trying to learn delegates but not able to understand the concepts ,kindly explain with code that how the same task can be done through delegates.Thanks in advance!

推荐答案

您可以通过块使用回调:

You can do using callback through blocks:

使用块的声明方法:

-(void)getResponse:(NSString *)url AndWithCallback:(void(^)(BOOL success, id responseObject))callback{
   if(data != nil){
       callback(YES,@"Your object");
   }
   else{
       callback(NO,@"pass nil");
   }
}

调用方法:

[self getResponse:@"" AndWithCallback:^(BOOL success, id responseObject) {
        NSLog(@"%@",responseObject);
    }];

这篇关于如何执行委托代替通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆