簡體   English   中英

UITextField resignFirstResponder工作有點然后不能

[英]UITextField resignFirstResponder working a bit then not

我在UiTableViewCell中添加一個UITextField *gasPrice; 像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM