UITextField resignFirstResponder工作了一点,然后就不工作了

|| 我在UiTableViewCell中添加这样的“ 0”:
- (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不同。 知道为什么这样做吗?谢谢!     
已邀请:
        我认为您正在使用gasField作为
property
。采用
self.gasField
    

要回复问题请先登录注册