Ios 如何将NSString从NSObject传递到UIViewcontrller?

Ios 如何将NSString从NSObject传递到UIViewcontrller?,ios,objective-c,Ios,Objective C,我知道这是一个非常基本的问题,但作为一个已经在谷歌搜索了大量资源的初学者,我就是做不到。我想将NSString从NSObject传递到UIViewController。我知道如果要将值传递给另一个类,我需要将其公开。我在DataConnection.m中有一个方法 - (void)jsonParse{ NSString* path = @"http://phdprototype.tk/getResultData.php"; NSURL* url = [NSURL URLWit

我知道这是一个非常基本的问题,但作为一个已经在谷歌搜索了大量资源的初学者,我就是做不到。我想将
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]];
但我得到了预期的标识符

“嗯~我试过一些代码,比如DataConnection*data= [DataConnection jsonParse];但我得到了未知类方法的错误 对于“jsonParse”ps的选择器:我已经将 -(void)jsonParse;在DataConnection.h–“

上述方法是调用方法的错误方法。你可以这样称呼它:

DataConnection*data=[[DataConnection alloc]init];
[数据解析]

但是根据你的问题,

“我想做的是将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"];
现在,recommendID将具有recData值。

您是否包括在内

 @synthesize recommendID;
在DataConnection.m文件中


我确实考虑过将此添加为注释,但我没有足够的评论来评论。< /P>您想将一个字符串从一个视图控制器移到另一个控制器<代码> DATECON= = [DATECONDISCONALOC] init;[dateCon jsonParse]并通过viewController中的dateCon变量访问您的属性。no~~~recData是数据库中的一个值,并且该方法位于NSObject中,而不是视图控制器。我遇到使用未声明标识符“dateCon”的错误??要让一个实例

A
与另一个实例
B
交互,您需要在
A
的实例中有一个可用的引用
*B
,然后您可以使用
A
中的
B
的公共变量、属性和方法。我已经把这些代码放在下面,DataConnection*data=[[DataConnection alloc]init];[data jsonParse];[self.delegate NextScreen:self ndx:[data.recommendID]];但是我需要uialertview的rData和lData!我可以从recData中获取值吗???我已经编辑了我的答案。检查它…在jsonParse中返回字典,并在view controller中提取必要的值。也许,我没有正确描述。rData和lData的值将在dataconnection中使用。这意味着这两个值应该保留在该c中类。viewcontroller将只接受recData。很抱歉误解!!您可以将它们与以下行一起使用…NSString*rData=[resultDic objectForKey:@“房间”];NSString*lData=[resultDic objectForKey:@“级别”];…并返回您在viewcontroller中需要的内容。。