Jon
Jon

Reputation: 4732

UITextField not saving text

I have a view with several UITextFields in it. I also have a high level UISegmentController that changes the view. When I enter text in a field, then press the hide keyboard button, then change segemnents, it saves fine. But when I enter text, and don't press the release keyboard button, then switch segments, it does not save the text.

How can I fix this?

Here is some of the code for the UITextField in my custom UITableViewCell:

- (IBAction)col2_doubleValueChanged:(id)sender
{
    NSString *newValue = [NSString stringWithFormat:@"%@/%@", self.col2_doubleEntryValue_1.text, self.col2_doubleEntryValue_2.text];
    NSMutableDictionary *result = [[[NSMutableDictionary alloc] init] autorelease];
    [result setValue:self.row_key forKey:@"row_key"];
    [result setValue:@"1" forKey:@"column"];
    [result setValue:newValue forKey:@"col2_value"];
    [[self delegate] editDidFinish:result];
    [[self delegate] valueChanged];
}

- (IBAction)col2_singleValueEditDidBegin:(id)sender
{
}

enter image description here

Edit:

- (void)valueChanged
{
    self.dirtyFlag = 1;
}

- (int)saveDataToServer
{
    [self.tableView resignFirstResponder];

    if (!dirtyFlag) {
        return 0;
    }    

    NSString *errors = [DataSource updatePatientWorkflowClinicalChecklistForAppointment:[[[DrChronoDataSource getCurrentAppointment] valueForKey:@"appointment_id"] intValue] clinicalInfo:self.clinicalChecklist checklistId:[self.clinicalChecklistId intValue] patientWorkflowServerId:patientWorkflowServerId];

    if ((NULL == errors) || ![errors isEqualToString: @""]) {
        //Show error messages.
        if (NULL == errors) {
            errors = @"Failed to save data to server.  Please retry.";
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status" message:errors delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return -1;
    } else {
        [self dismissModalViewControllerAnimated:FALSE];
    }

    self.dirtyFlag = 0;
    return 0;
}

Upvotes: 1

Views: 426

Answers (3)

eric.mitchell
eric.mitchell

Reputation: 8855

Couldn't you link both events to the same action? Maybe you could make a BOOL in your header file called needsSaving. Set it to NO every time editing begins, then YES every time the data is saved successfully. Then only execute the body of your saving methods if needsSaving is NO, that will avoid unnecessary saving. I think doing this would simplify this function of your app. i.e.

- (IBAction)col2_singleValueEditDidBegin:(id)sender {
    needsSaving = YES;
}

- (int)saveDataToServer
{
    [self.tableView resignFirstResponder];

    if (!dirtyFlag || !needsSaving) {
        return 0;
    }

    NSString *errors = [DataSource updatePatientWorkflowClinicalChecklistForAppointment:[[[DrChronoDataSource getCurrentAppointment] valueForKey:@"appointment_id"] intValue] clinicalInfo:self.clinicalChecklist checklistId:[self.clinicalChecklistId intValue] patientWorkflowServerId:patientWorkflowServerId];

    if ((NULL == errors) || ![errors isEqualToString: @""]) {
        //Show error messages.
        if (NULL == errors) {
            errors = @"Failed to save data to server.  Please retry.";
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status" message:errors delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return -1;
    } else {
        needsSaving = NO;
        [self dismissModalViewControllerAnimated:FALSE];
    }

    self.dirtyFlag = 0;
    return 0;
}

Upvotes: 0

jstevenco
jstevenco

Reputation: 2953

Looks to me like you are not implementing the UITextFieldDelegate prototocols.

Upvotes: 0

WrightsCS
WrightsCS

Reputation: 50727

You do not have any writing functions.

- (IBAction)col2_doubleValueChanged:(id)sender
{
    NSString *newValue = [NSString stringWithFormat:@"%@/%@", self.col2_doubleEntryValue_1.text, self.col2_doubleEntryValue_2.text];
    NSMutableDictionary *result = [[[NSMutableDictionary alloc] init] autorelease];
    [result setValue:self.row_key forKey:@"row_key"];
    [result setValue:@"1" forKey:@"column"];
    [result setValue:newValue forKey:@"col2_value"];

    /* your aren't writing the new data anywhere, 
       you need to add something like this: 
     */

    [result writeToFile: @"somefile.txt" atomically: YES];

    [[self delegate] editDidFinish:result];
    [[self delegate] valueChanged];
}

Upvotes: 1

Related Questions