ellieinphilly
ellieinphilly

Reputation: 477

How to code the delegate pattern in objective C

I'm trying to pass JSON to VenueIDController by implementing delegate/protocols. however it doesn't seem to work. Please let me know what I'm missing. I even put in an log in the fetch method to see if it is getting called but it doesn't even log which means it isn't getting called please help.

I have a FetchVenuesViewController.h

#import "VenueTableViewController.h" 
#import "VenueIDController.h"

@interface FetchVenuesViewController : UIViewController< VenueTableViewControllerDelegate, VenueIDControllerDelegate>{
    NSDictionary* venueJSON;
    NSDictionary* idJSON;

};

@property (strong) NSDictionary* idJSON;


- (void)VenueFetch;

- (void)IDFetch;


@end

In FetchVenuesViewController.m

@synthesize idJSON;

- (void)IDFetch {

    //request some webservice 


    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                 error:&error];
    //save the response



    if (data) {

        id IDJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        if (!IDJSON) {
           //handle error

        }
        else {


        //do something

        }



    } else {
        // fetch failed

    }

    activityIndicator.hidden = NO;
}

-(NSDictionary *)getID{
    [self IDfetch];
    NSLog(@"json%@",idJSON);
    return idJSON;
}

In VenueIDController.h

@protocol VenueIDControllerDelegate;

@interface VenueIDController : UIViewController{

}


@property (assign) id <VenueIDControllerDelegate> delegate;
-(IBAction)getIDData:(id)sender;

@end

@protocol VenueIDControllerDelegate <NSObject>
-(NSDictionary *)getID;
@end

and in VenueIDController.m

@interface VenueIDController (){
    NSMutableArray* IDData;
    UIImage* IDBarcode;
}
-(void) displayIDData:(NSDictionary*)data;
@end

@implementation VenueIDController
@synthesize delegate;


-(void) displayIDData:(NSDictionary*)data{

    [delegate getID];


    NSDictionary* idJSON = data;


}

Upvotes: 1

Views: 338

Answers (1)

Abizern
Abizern

Reputation: 150615

This seems wrong:

-(void) displayIDData:(NSDictionary*)data{

    [delegate getID];


    NSDictionary* idJSON = data;


}

You are calling the delegate method and throwing away its result.

Upvotes: 2

Related Questions