UITextField resignFirstResponder工作了一点然后没有

时间:2011-06-23 19:13:01

标签: ios uitableview uitextfield resignfirstresponder

我在这里添加一个UITextField *gasPrice;的UiTableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier;
    if (indexPath.section == 0 && indexPath.row < [self.userCarsArray count])
        CellIdentifier = [NSString stringWithFormat:@"Cell%d%d%@", indexPath.section, indexPath.row, [[self.userCarsArray objectAtIndex:indexPath.row] idCar]];
    else if (indexPath.section == 0 && indexPath.row == [self.userCarsArray count])
        CellIdentifier = [NSString stringWithFormat:@"Cell%d%d%@", indexPath.section, indexPath.row, @"AddCar"];
    else
        CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        if (indexPath.section == 1 && indexPath.row == 0) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }
        else if (indexPath.section == 2 && indexPath.row == 2) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }
        else {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        if (indexPath.section == 1 && indexPath.row == 1) {
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 10, 125, 30)];
            textField.adjustsFontSizeToFitWidth = YES;
            textField.textColor = [UIColor colorWithRed:0.20f green:0.30f blue:0.49f alpha:1.0f];
            textField.placeholder = @"0.00";
            textField.keyboardType = UIKeyboardTypeDecimalPad;
            textField.returnKeyType = UIReturnKeyDone;
            textField.backgroundColor = [UIColor whiteColor];
            textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
            textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
            textField.textAlignment = UITextAlignmentRight;
            textField.tag = 0;
            textField.delegate = self;
            textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
            [textField setEnabled: YES];

            [cell addSubview:textField];

            [textField release];

            gasField = textField;
        }
    }

    // Selection style.
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    // Vehicles cells.
    if (indexPath.section == 0) {
        // ...
    }

    // General cells.
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"Measurement System";
            cell.textLabel.textColor = [UIColor darkGrayColor];

            if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
                cell.detailTextLabel.text = @"Miles";
            else
                cell.detailTextLabel.text = @"Meters";
        }

        if (indexPath.row == 1) {
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
                cell.textLabel.text = @"Gas Price (gal)";
            else
                cell.textLabel.text = @"Gas Price (L)";

            // Gas price.
            if ([EcoAppAppDelegate gasPrice] != 0.00)
                gasField.text = [NSString stringWithFormat:@"%.2f", [EcoAppAppDelegate gasPrice]];
        }
    }

    // Information cells.
    if (indexPath.section == 2) {
        // ...
    }

    return cell;
}

然后,为了在用户完成时管理键盘(因为我使用没有返回键的键盘),我有这些功能:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] 
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)] autorelease];
    [self.navigationItem setLeftBarButtonItem:doneButton animated:YES];

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];

    // Save gas price.
    if ([textField.text isEqualToString:@""])
        [EcoAppAppDelegate setGasPrice:0.00];
    else
        [EcoAppAppDelegate setGasPrice:[textField.text floatValue]];

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Hide keyboard.
    [textField resignFirstResponder];

    return YES;
}

导航栏按钮的功能“完成”:

- (void)done:(id)sender {
if ([gasField canResignFirstResponder]) {
    [gasField resignFirstResponder];
}

[self.navigationItem setLeftBarButtonItem:nil animated:YES];

}

所以它首先工作正常,当我留在我的设置视图控制器,但当我在其他一些单元格上执行其他操作(推送其他视图控制器,返回等)时,它会停止工作。可能是因为gasField指针消失了或类似的东西,gasField对象仍然与nil不同。

知道为什么这样做吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您使用的是gasField property。使用

self.gasField