UITextField不保存文本

时间:2011-12-06 20:41:39

标签: iphone objective-c cocoa-touch uitextfield uisegmentedcontrol

我有一个包含多个UITextFields的视图。我还有一个更高级别UISegmentController来更改视图。当我在字段中输入文本时,然后按隐藏键盘按钮,然后更改segemnents,它保存正常。但是当我输入文字,并且没有按下释放键盘按钮,然后切换片段时,它不会保存文本。

我该如何解决这个问题?

以下是我的自定义UITextFieldUITableViewCell的部分代码:

- (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

编辑:

- (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;
}

3 个答案:

答案 0 :(得分:1)

您没有任何写作功能。

- (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];
}

答案 1 :(得分:0)

你不能将两个事件都链接到同一个动作吗?也许你可以在头文件中创建名为needsSaving的BOOL。每次编辑开始时将其设置为NO,然后在每次成功保存数据时将其设置为YES。然后,如果needsSaving为NO,则仅执行保存方法的主体,这将避免不必要的保存。我认为这样做可以简化您的应用程序的这个功能。即。

- (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;
}

答案 2 :(得分:0)

让我觉得你没有实施UITextFieldDelegate协议。