Trying to save data in Core Data from UITableViewController textbox

╄→尐↘猪︶ㄣ 提交于 2019-12-11 20:13:18

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/12263517/trying-to-save-data-in-core-data-from-uitableviewcontroller-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!