我知道这是一个非常基本的问题,但作为一个初学者,我已经谷歌了很多资源,我无法做到。我想将NSString
从NSObject
传递给UIViewController
。我知道是否要将值传递给另一个我需要公开的类。我在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;
}
我想要做的是将recData的值传递给另一个UIViewController
。所以我在' h:
@property (nonatomic) NSString *recommendID;
在我的UIViewController中我有这段代码:
[self.delegate NextScreen: self ndx: recData];
我想将值传递给reData。有谁告诉我该怎么做?对不起我的程序知识!
~~~更新~~~ 我把这样的代码放在了:
DataConnection *data = [[DataConnection alloc] init];
[data jsonParse];
[self.delegate NextScreen: self ndx: [data.recommendID]];
但我得到了预期的标识符?
答案 0 :(得分:2)
“mmmmmm~我尝试过像DataConnection * data =这样的代码 [DataConnection jsonParse];但是我得到了没有已知类方法的错误 对于“jsonParse”ps的选择器:我已经放了 - (void)jsonParse;在DataConnection.h中 - “
上面提到的方法是调用方法的错误方法。你可以这样称呼它:
DataConnection *data = [[DataConnection alloc] init];
[data jsonParse];
但是,根据你的问题,
“我想要做的是将recData的值传递给另一个 UIViewController中。所以我在'h:“
中制作了一个代码
将jsonParse的返回类型更改为NSDictionary并返回您要传输的resultDic。
- (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;
}
在视图控制器中,
@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"];
现在recommendedID将具有recData值。
答案 1 :(得分:0)
你有没有
@synthesize recommendID;
DataConnection.m文件中的?
我确实考虑过将此添加为评论,但我没有足够的评论声誉。