开发者

Pass JSON output to Another Class - iPhone

I have two classes:

dbConnector - connects to database and retrieves data viewcontroller - instantiates dbC开发者_如何学JAVAonnector and calls data retrieval functions

The problem I am facing is getting dbConnector to pass the data to the viewcontroller:

dbConnectorClass:

#import "dbConnector.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h";

@implementation dbConnector
@synthesize data;

//method to 
-(void)getQuestions:(NSString*)sectionId from:(NSString*)url{
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link]; 
    [request setPostValue:sectionId forKey:@"section"]; 
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];    
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSData *responseData = [request responseData];
    NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSDictionary *questions = [json objectFromJSONString];
    NSLog(@"%@", questions);
    //need to pass questions to view controller...how??
    [json release];
}

@end

viewcontroller class:

#import "dbQuestionGetterViewController.h"
#import "dbConnector.h";

@implementation dbQuestionGetterViewController
@synthesize questions, db;

-(void)viewDidLoad{
    db = [[dbConnector alloc]init];
    //code to initialise view
    [db getQuestions:@"2" from:@"http://dev.speechlink.co.uk/David/get_questions.php"];
    //self.questions = arr;
    [super viewDidLoad];
}

What I would like is to assign the NSDictionary* questions from dbConnector to the questions pointer synthesized in my viewcontroller.m

How can I get dbConnector to pass the returned JSON string to my view controller viewDidLoad method?


You could implement and delegate protocol for dbConnector class and make the dbQuestionGetterViewController the delegate of the dbConnector instance.

Edit:

Delegation

Protocol

Edit 2:

In dbConnector.h add before import

@protocol dbConnectorDelegate 

-(void)didFinishDownloadData:(NSData*)data;

@end

In dbConnector.h in @interface add:

id<dbConnectorDelegate> delegate;

And declare a new property for delegate

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

In your dbConnector.m file synthesize the delegate property:

@synthesize delegate;

And in requestFinished add

[delegate didFinishDownloadData: responseData];

By this you are telling to your delegate that you have finish download with the data parameter.

In your dbQuestionGetterViewController.h import the dbConnector.h and tell to the dbQuestionGetterViewController that will implement dbConnectorDelegate:

@interface dbQuestionGetterViewController : UIViewController<dbConnectorDelegate>

And in your dbQuestionGetterViewController.m add

- (void)didFinishDownloadData:(NSData*)data{
        NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSDictionary *questions = [json objectFromJSONString];
        NSLog(@"%@", questions);
        //need to pass questions to view controller...how??
        [json release];
    }

And your view did load add:

db.delegate = self;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜