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

发布于 2024-11-16 21:59:56 字数 5272 浏览 7 评论 0原文

我在 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不同。

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

I add in a UiTableViewCell a UITextField *gasPrice; like this:

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

Then, to manage the keyboard when the user is done (because I use a keyboard without return key), I have those functions:

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

And the function for the navigation bar button "Done":

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

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

}

So it works fine first, when I stay in my settings view controller, but when I do other stuff on some other cells (that push other view controllers, come back, etc), it stops working. It is probably because the gasField pointer has disappeared or something like that, the gasField object tho is still different from nil.

Any idea why it does that? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2024-11-23 21:59:56

我认为您正在使用gasField作为属性。使用

self.gasField

I think you are using gasField as a property. use

self.gasField
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文