简体   繁体   English

尝试从UITableViewController文本框中将数据保存在Core Data中

[英]Trying to save data in Core Data from UITableViewController textbox

I have a Table view controller with 5 textboxes in a row which are added with null values when we click on add button. 我有一个表格视图控制器,其中连续有5个文本框,当我们单击添加按钮时,这些文本框会添加空值。

I am trying to save the data in a database via Core Data. 我正在尝试通过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 -(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. didEndEditing表示您按下了返回键或将响应者移动到了另一个对象。

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. 例如,每次更改文本字段中的字符时,都会触发此方法textField:shouldChangeCharactersInRange:replacementString: 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] . 要告诉下一个文本框成为第一个响应者,请调用[mySecondField becomeFirstResponder]

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM