Reputation: 125
I understand this is a very basic question, but as a beginner who already google a bunch of resources I just couldn't make it. I would like to pass a NSString
from a NSObject
to UIViewController
. I know if want to pass a value to another class I need to make it public. I have a method in DataConnection.m
- (void)jsonParse{
NSString* path = @"http://phdprototype.tk/getResultData.php";
NSURL* url = [NSURL URLWithString:path];
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSDictionary* resultDic = [dic objectForKey:@"maxid"];
NSString* recData = [resultDic objectForKey:@"recommendData"];
NSString* rData = [resultDic objectForKey:@"room"];
NSString* lData = [resultDic objectForKey:@"level"];
NSLog(@"recommendData = %@, room = %@, level = %@",recData,rData,lData);
self->_room = rData;
self->_level = lData;
self->_recommendID = recData;
}
what I want to do is to pass the value of recData to another UIViewController
. So I have made a code in 'h:
@property (nonatomic) NSString *recommendID;
in my UIViewController I have this code:
[self.delegate NextScreen: self ndx: recData];
I would like pass value to reData. Anyone tell me how to do that?? sorry for my programme knowledge!
~~~ update~~~ I have put the codes like this:
DataConnection *data = [[DataConnection alloc] init];
[data jsonParse];
[self.delegate NextScreen: self ndx: [data.recommendID]];
but i got expected identifier??
Upvotes: 0
Views: 218
Reputation: 193
"mmmmmm~i have try some code like DataConnection *data = [DataConnection jsonParse]; but i got error of no known class method for selector of "jsonParse" ps: i have put - (void)jsonParse; in DataConnection.h –"
Above mentioned way is wrong way of calling a method. you can call it this way:
DataConnection *data = [[DataConnection alloc] init];
[data jsonParse];
But, as per your question,
"what I want to do is to pass the value of recData to another UIViewController. So I have made a code in 'h:"
Change the return type of jsonParse to NSDictionary and return the resultDic which you wish to transfer.
- (NSDictionary *)jsonParse{
NSString* path = @"http://phdprototype.tk/getResultData.php";
NSURL* url = [NSURL URLWithString:path];
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSDictionary* resultDic = [dic objectForKey:@"maxid"];
/* NSString* recData = [resultDic objectForKey:@"recommendData"];
NSString* rData = [resultDic objectForKey:@"room"];
NSString* lData = [resultDic objectForKey:@"level"];
NSLog(@"recommendData = %@, room = %@, level = %@",recData,rData,lData);
self->_room = rData;
self->_level = lData;
self->_recommendID = recData;*/
return resultDic;
}
In your view controller,
@property (nonatomic,retain) NSDictionary *resultDict;
@synthesize resultDict;
DataConnection *data = [[DataConnection alloc] init];
self.resultDict = (NSDictionary *)[data jsonParse];
_recommendID = [resultDic objectForKey:@"recommendData"];
_room = [resultDic objectForKey:@"room"];
_level = [resultDic objectForKey:@"level"];
now recommendID will have the recData value.
Upvotes: 2
Reputation: 3
have you included
@synthesize recommendID;
in the DataConnection.m file?
I did consider adding this as a comment, but i dont have enough reputation for commenting.
Upvotes: 0