Iphone iOS-如何使用委托打印文本字段中的值

Iphone iOS-如何使用委托打印文本字段中的值,iphone,ios,ipad,ios6,delegates,Iphone,Ios,Ipad,Ios6,Delegates,我是iOS新手。我有点问题,你有什么解决办法吗?我无法在textfield中打印json值 这是我的contactViewController.m文件: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. fname.text=@"Hello";

我是iOS新手。我有点问题,你有什么解决办法吗?我无法在textfield中打印json值 这是我的
contactViewController.m
文件:

  - (void)viewDidLoad
    {
        [super viewDidLoad]; 
        // Do any additional setup after loading the view, typically from a nib.

        fname.text=@"Hello";
    }
-(IBAction)list:(id)sender{
    vedant=[[WebserviceViewController alloc]init];
    vedant.delegate=self;
    [vedant listContacts];

}
加载视图后,文本框中会显示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
    }

}
-(void)所有联系人:(NSString*)JSONResponse{
NSLog(@“%@”,JSONResponse);
NSData*jsonData=[JSONResponse dataUsingEncoding:NSASCIIStringEncoding];
//
n错误*错误;
NSDictionary*json=[NSJSONSerialization JSONObjectWithData:jsonData选项:针织错误:&err];
int accCount=[json count];

对于(int i=0;i检查
fname
是否为
nil
。如果您忘记在Interface Builder中正确连接它,
fname
将为
nil
,所有发送到它的消息(如
setText:
)将被忽略。

请首先检查是否已将文本字段与fname变量绑定。如果是,请尝试使用以下方法赋值:

fname.text=[NSString stringWithFormat:@"%@",firstName];
试试这个。我希望这对你有用。

将你的名字(即来自json的值)转换成全局值,并将该全局值分配到你的文本字段中。这很简单。如果你仍有疑问,我将发布示例代码txtName.text=firstname;或者
text=[NSString stringWithFormat:@“%@”,firstName];

使用委托,我解决了这个问题

在我的
contactViewController.m
文件中:

  - (void)viewDidLoad
    {
        [super viewDidLoad]; 
        // Do any additional setup after loading the view, typically from a nib.

        fname.text=@"Hello";
    }
-(IBAction)list:(id)sender{
    vedant=[[WebserviceViewController alloc]init];
    vedant.delegate=self;
    [vedant listContacts];

}
webserviceViewController.h
中,我创建了自己的委托

@protocol WebservicesDelegate <NSObject>
-(void)allContacts:(NSString *)JSONResponse;
@end
编译器返回到
contactViewController.m
文件中的
allContacts
函数。此代码相同。我现在没有更改代码,它会打印值

-(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
}

}
-(void)所有联系人:(NSString*)JSONResponse{
NSLog(@“%@”,JSONResponse);
NSData*jsonData=[JSONResponse dataUsingEncoding:NSASCIIStringEncoding];
//
n错误*错误;
NSDictionary*json=[NSJSONSerialization JSONObjectWithData:jsonData选项:针织错误:&err];
int accCount=[json count];

对于(int i=0;ifname等于nil?函数
allContacts:
在webserviceViewController.m中实现,您想在contactViewController.m中的
fname
中显示值,我是对的吗?是的,并且fname值为nil,即使在视图加载时输入了一些值,函数
allContacts
将“仅”在这个文本字段上显示一个值,或者这只是一个测试?您的错误是,您正在将该值分配给一个不存在的文本字段,
fname
引用webserviceViewController.m的属性,而不是contactViewController.m的字段visibleNo,它不打印任何值,但现在它在fname fi中显示Hello埃尔德。