gpt4 book ai didi

ios - 如何将 NSString 从 NSObject 传递到 UIViewcontrller?

转载 作者:行者123 更新时间:2023-11-28 22:12:56 27 4
gpt4 key购买 nike

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

但我得到了预期的标识符??

最佳答案

"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 –"

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

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

但是,根据你的问题,

"what I want to do is to pass the value of recData to another UIViewController. So I have made a code in '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;
}

在你的 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"];

现在 recommendID 将具有 recData 值。

关于ios - 如何将 NSString 从 NSObject 传递到 UIViewcontrller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com