【发布时间】:2013-03-04 09:10:34
【问题描述】:
我是 iOS 新手。我有一些问题你有解决方案吗?我无法在文本字段中打印 json 值
这是我的contactViewController.m 文件:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
fname.text=@"Hello";
}
视图加载后 hello 值显示在文本框中,但是当我调用列表按钮获取 json 值时:
-(IBAction)list:(id)sender{
vedant=[[WebserviceViewController alloc]init];
[vedant listContacts];
}
然后从 webserviceViewController.m 我将 jsonResponse 传递给同一个文件,即 contactViewController.m 并解析 json 值并打印它,但它没有在文本字段中显示值
-(void)allContacts:(NSString *)JSONResponse{
NSLog(@"%@",JSONResponse);
NSData *jsonData = [JSONResponse dataUsingEncoding:NSASCIIStringEncoding];
//
NSError *err;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&err];
int accCount =[json count];
for(int i=0;i<accCount;i++){
NSData *jsonData = [JSONResponse dataUsingEncoding:NSASCIIStringEncoding];
//
// NSLog(@"%@",JSONResponse);
NSError *err;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&err];
NSString *firstName = [NSString stringWithFormat:@"%@", [[json objectAtIndex:i] objectForKey:@"first_name"]];
NSLog(@"%@",firstName); //this prints the actual first name in console But
fname.text=firstName;
NSLog(@"%@",fname.text); //it prints "(Null)" in console
}
}
我需要创建委托来传递值吗?
是的,那么创建这样的委托的任何帮助文章或示例。
【问题讨论】:
-
fname 等于 nil?
-
函数
allContacts:是在webserviceViewController.m 中实现的,你想在fname中显示contactViewController.m 中的值,对吗? -
是的,即使我在视图加载时输入了一些值,fname 的值也是 nil
-
函数
allContacts将“仅”在此文本字段上显示一个值,或者这只是一个测试? -
您遇到的错误是您将值分配给不存在的 textField,
fname引用 webserviceViewController.m 的属性,而不是 contactViewController.m 的字段可见
标签: iphone ios ipad ios6 delegates