Lovely Lovely - 1 month ago 11
Objective-C Question

Communication between controllers through Custom delegates

I have obtained response from server in a

Dictionary
. Now, I have a button on
ViewControllerA
. On the click of that button, I want to show the details obtained from
dictionary
on
tableViewCells
of
ViewControllerB
. I have already achieved this task through
Notifications
but I am not able to understand that how same task can be done through
delegates
and it is required to implement through
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
having button:

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


Kindly explain with code how the task can be achieved through
delegates
? I am in my initial phase of development... after reading several tutorials then also couldn't understand
delegates
.

Answer

I will help you

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];    
}
Comments