Lovely Lovely - 1 month ago 7
Objective-C Question

Sending dictionary from a VC to another VC through Custom delegtes

I have a class in which response from server is coming as responseDictionary.I want to use this responseDictionary in another VC but not able to send the dictionary through delegates.Kindly explain with code in objective c that how this can be achieved.

Answer

sample project, it is simply for demo, u can refer it

first we will create a class names WebService it will request the data, and as a response it will get a dictionary, and also it defines a delegate to notify that data is received and sent to the VC (in your case)

in WebService.h

#import <Foundation/Foundation.h>

@protocol WebServiceDelegate;
@interface WebService : NSObject
{

}
@property(nonatomic, assign) id<WebServiceDelegate>webServiceDelegate;

- (void)webserviceRequestData; //initiates the data request
@end


@protocol WebServiceDelegate <NSObject>
- (void)webService:(WebService *)aWebService didReceievResponseData:(NSDictionary *)dataDictionary;
@end

in WebService.m

#import "WebService.h"

@implementation WebService

- (instancetype)init
{
   self = [super init];
   if(self)
   {

   }
   return self;
}

- (void)webserviceRequestData
{
   sleep(5); //for demo 
   [self makeWebserviceToGetSomeData];
}

- (void)makeWebserviceToGetSomeData
{
   NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:@"Data_1",@"Key_1",@"Data_2",@"Key_2",@"Data_3",@"Key_3", nil];
  [self receievedSomeData:data];
}

 //finally data is received as dictionary and send it to VC
- (void)receievedSomeData:(NSDictionary *)dataDictionary
{
    if([self.webServiceDelegate respondsToSelector:@selector(webService:didReceievResponseData:)])
        [self.webServiceDelegate webService:self didReceievResponseData:dataDictionary];
}

@end

and in the view controller class ViewController.h

#import <UIKit/UIKit.h>
#import "WebService.h"

@interface ViewController : UIViewController<WebServiceDelegate>
@property(nonatomic, strong) WebService *myWebService; 
@end

and in ViewController.m

 #import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 - (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
   _myWebService = [[WebService alloc] init];
   _myWebService.webServiceDelegate = self; //this is call back to this class
 }

 - (void)viewDidAppear:(BOOL)animated
 {
   [super viewDidAppear:animated];
   [_myWebService webserviceRequestData];
 }

 - (void)webService:(WebService *)aWebService didReceievResponseData:(NSDictionary *)dataDictionary
{
   NSLog(@"web data:%@",dataDictionary.description);
}


- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

after 5 seconds we made to call the method makeWebserviceToGetSomeData and it will send the call the another method receievedSomeData: method in this we are checking weather caller implemented the method or not to avoid crash(unrecognised selector sent to object), and we are passing the data (dictionary) to view controller

and also u can call web service from any class (any viewcontroller) and set it as a delegate