如何在目标C中编写委托模式

时间:2012-10-20 23:39:20

标签: objective-c ios delegates protocols

我正在尝试通过实现委托/协议将JSON传递给VenueIDController。但它似乎没有用。请让我知道我错过了什么。 我甚至在fetch方法中输入一个日志来查看它是否被调用,但它甚至没有记录,这意味着它没有被调用请求帮助。

我有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

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

VenueIDController.h

@protocol VenueIDControllerDelegate;

@interface VenueIDController : UIViewController{

}


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

@end

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

并在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;


}

1 个答案:

答案 0 :(得分:2)

这似乎不对:

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

    [delegate getID];


    NSDictionary* idJSON = data;


}

您正在调用委托方法并丢弃其结果。