尝试从UITableViewController文本框保存Core Data中的数据 [英] Trying to save data in Core Data from UITableViewController textbox

查看:76
本文介绍了尝试从UITableViewController文本框保存Core Data中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表视图控制器,在一行中有5个文本框,当我们点击添加按钮时,它们都添加了空值。



我试图保存数据在数据库中通过Core Data。



我的问题是,当我在文本框中单击并完成设置值,并通过触摸屏,当我试图把数据光标移动到另一个文本框,但不允许我输入文本,直到我按键盘上的Return键。



这是我的文本框编码:



- (void)textFieldDidEndEditing:(UITextField *)textField

  {

row1 = textField.tag;

NSLog(@Row is%d,row1);

path = [NSIndexPath indexPathForRow:row1 inSection:0];

Input_Details * inputDetailsObject1 = [self.fetchedResultsController objectAtIndexPath:path];

/ *
从文本字段中的值更新输入值。
* /

EditingTableViewCell * cell;

cell =(EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row1 inSection:0]];
inputDetailsObject1.mh_Up = cell.cell_MH_up.text;
inputDetailsObject1.mh_down = cell.cell_MH_dn.text;
inputDetailsObject1.sewer_No = [NSNumber numberWithInt:[cell.cell_Sewer_No.text intValue]];
inputDetailsObject1.mhup_gl = [NSNumber numberWithFloat:[cell.cell_gl_MHUP.text floatValue]];
inputDetailsObject1.mhdn_gl = [NSNumber numbersWithFloat:[cell.cell_gl_MHDN.text floatValue]];
inputDetailsObject1.pop_Ln = [NSNumber numberWithInt:[cell.cell_Line_pop.text intValue]];
inputDetailsObject1.sew_len = [NSNumber numberWithFloat:[cell.cell_Sewer_len.text floatValue]];
[self saveContext];
[textField resignFirstResponder];
NSLog(@保存MH_up值%@保存在索引路径%d的保存,inputDetailsObject1.mh_Up,row1);




}

- (void)saveContext

{
$ b b NSManagedObjectContext * context = [self.fetchedResultsController managedObjectContext];

NSError * error = nil;

if(![context save:& error])

{

NSLog(@未解析的错误%@,%@ error,[error userInfo]);
abort();
}


}

以下是表格视图和文本框的图片

$ b $ c $>

$ <$>

$ <$>

$

b

如果你想通过一些其他机制移动到下一个文本框,那么你需要实现一些其他委托方法。例如,此方法 textField:shouldChangeCharactersInRange:replacementString:每次更改文本字段中的字符时触发。



要让下一个文本框成为第一个响应者,请调用 [ mySecondField becomeFirstResponder]



希望这有助于。


I have a Table view controller with 5 textboxes in a row which are added with null values when we click on add button.

I am trying to save the data in a database via Core Data.

My problem is that when I click in the text box and finish putting the value and via a touch screen when i am trying to put the data the cursor goes to the other text box but do not allow me to enter text until I press the Return key on the keyboard.

Here is my coding for the text box:

-(void)textFieldDidEndEditing:(UITextField *)textField

{

   row1=textField.tag;

    NSLog(@"Row is %d",row1);

    path = [NSIndexPath indexPathForRow:row1 inSection:0];

    Input_Details *inputDetailsObject1=[self.fetchedResultsController objectAtIndexPath:path];

    /*
     Update the Input Values from the values in the text fields.
     */

    EditingTableViewCell *cell;

    cell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row1 inSection:0]];
    inputDetailsObject1.mh_Up= cell.cell_MH_up.text;
    inputDetailsObject1.mh_down=cell.cell_MH_dn.text;
    inputDetailsObject1.sewer_No=[NSNumber numberWithInt:[cell.cell_Sewer_No.text intValue]];
    inputDetailsObject1.mhup_gl=[NSNumber numberWithFloat:[cell.cell_gl_MHUP.text floatValue]];
    inputDetailsObject1.mhdn_gl=[NSNumber numberWithFloat:[cell.cell_gl_MHDN.text floatValue]];
    inputDetailsObject1.pop_Ln=[NSNumber numberWithInt:[cell.cell_Line_pop.text intValue]];
    inputDetailsObject1.sew_len=[NSNumber numberWithFloat:[cell.cell_Sewer_len.text floatValue]];
    [self saveContext];
    [textField resignFirstResponder];
    NSLog(@"Saving the MH_up value %@ is saved in save at index path %d",inputDetailsObject1.mh_Up,row1);




}

-(void)saveContext

{

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

 NSError *error = nil;

if (![context save:&error]) 

{

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


}

Here is a picture of the table view and text boxes.

解决方案

didEndEditing means you pressed the return key or moved the responder to another object.

If you want to move to the next text box by some other mechanism then you need to implement some other delegate method. For example, this method textField:shouldChangeCharactersInRange:replacementString: fires every time you change a characters in a textfield. You can use it to change the responder based on the input for example.

To tell the next text box to become the first responder you call [mySecondField becomeFirstResponder].

hope this helps.

这篇关于尝试从UITableViewController文本框保存Core Data中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆