【问题标题】:iOS - How to print value in textfield using delegateiOS - 如何使用委托在文本字段中打印值
【发布时间】: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


【解决方案1】:

检查fname 是否不是nil。如果您忘记在 Interface Builder 中正确连接它,fname 将是 nil,并且发送给它的所有消息(如 setText:)都将被忽略。

【讨论】:

    【解决方案2】:

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

    fname.text=[NSString stringWithFormat:@"%@",firstName];
    

    试试这个。我希望这对你有用。

    【讨论】:

    • 不,它不打印任何值,但现在它在 fname 字段中显示 Hello。
    【解决方案3】:

    将您的名字(即来自 json 的值)获取到全局值并将该全局值分配给您的 textfield 。就是这么简单。如果您仍然有疑问而不是回复,我会发布示例代码

    【讨论】:

      【解决方案4】:

      txtName.text=名字;或者 txtName.text=[NSString stringWithFormat:@"%@",firstName];

      【讨论】:

        【解决方案5】:

        使用 Delegate 我解决了这个问题

        在我的contactViewController.m 文件中:

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

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

        @protocol WebservicesDelegate <NSObject>
        -(void)allContacts:(NSString *)JSONResponse;
        @end
        

        然后在webserviceViewController.m 文件中从后端获取 json 响应后,我将使用此代码将响应发送回我的contactViewController.m 文件

        [self.delegate allContacts:jsonResponse];
        

        编译器返回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
        }
        
        }
        

        我认为因为我试图从不同的视图获取数据并希望在另一个视图中显示数据是问题所在。 但是通过创建委托,问题得到了解决,我可以轻松地将数据从一个视图发送到另一个视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多