0
我已經從服務器獲得響應Dictionary
。現在,我在ViewControllerA
上有一個按鈕。點擊該按鈕後,我想要顯示從dictionary
獲得的詳細信息的tableViewCells
。我已通過Notifications
完成了此任務,但我無法理解如何通過delegates
完成相同的任務,並且需要通過delegates
執行。通過自定義代表控制器之間的通信
#####Code for Network Class
-(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];
}
ViewControllerClass
有按鈕:
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyReload:) name:@"ReloadNotification" object:nil];
}
請與代碼解釋如何將任務可以通過delegates
實現?我正處於開發的最初階段......在閱讀了幾篇教程之後,也無法理解delegates
。
你試圖做的事情既不需要委託,也不需要通知。你爲什麼想用'delegates'來做?這是一個任務還是什麼? – Adeel