2016-10-24 51 views
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

+0

你試圖做的事情既不需要委託,也不需要通知。你爲什麼想用'delegates'來做?這是一個任務還是什麼? – Adeel

回答

1

我會幫你

ViewControllerA.h

#import <UIKit/UIKit.h> 
@class ViewControllerA; 
@protocol ViewControllerADelegate <NSObject> 

- (void)viewControllerA:(ViewController *)viewControllerA showDictinaoryDataOnTableView:(NSDictionary *)dict; 

@end 

@interface ViewControllerA : UIViewController 

@property (nonatomic, assign)id<ViewControllerDelegate> delegate; 

- (IBAction)doneButtonTapped:(id)sender; 
@end 

ViewControllerA.m

#import "ViewControllerA.h" 
#import "ViewControllerB.h" 
@interface ViewControllerA() 

@end 

@implementation ViewControllerA 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 


- (IBAction)actionGoNext:(id)sender; 
{ 
    [self.delegate viewControllerA:self showDictinaoryDataOnTableView:dict]; //Here pass your dict 
    ViewControllerB *vcB = [ViewController alloc] initWithNibName:@"ViewControllerB" bundle:nil; 
    [self.navigationController pushViewController:vcB animated:YES]; 
} 

ViewControllerB.h

#import <UIKit/UIKit.h> 
#import "ViewControllerB.h" 
@interface ViewControllerB : UIViewController <ViewControllerADelegate> 
@property (nonatomic, strong) IBOutlet UITableView *tblDictData; 
@end 

ViewController.B

#import "ViewControllerB.h" 

@interface ViewControllerB() 

@end 

@implementation ViewControllerB 

@synthesize tblDictData; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (void)viewControllerA:(ViewControllerA *)viewControllerA showDictinaoryDataOnTableView:(NSDictionary *)dict 
{ 
    yourdict = dict;  
    ... Must Do your stuff for showing data from dictionary to table view here 
    //Then reload the table view 
    [tblDictData reloadData];  
}