iPad 不放弃响应者

发布于 2024-09-28 11:19:58 字数 1371 浏览 1 评论 0原文

我有一个表,其中将 3 个 UITextField 添加到单元格的内容视图中(每个部分 1 行)。我插入了第四部分,以便我可以将特定字段滚动到键盘上方。

我遇到的问题(仅在 iPad 上)是,当其中一个文本字段具有firstResponder 时,当用户点击另一个字段时,它不会放弃它。 ipad 上的响应者链有差异吗?在我的视图控制器 UITextFieldDelegate 的以下代码中 - 当触摸另一个字段时,不会调用 textFieldShouldEndEditing。按完成按钮将按预期工作(除非已触摸另一个字段)。

下面的代码有什么问题吗?

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!_editing++) {
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }

    // scroll to section number in the text fields tag
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:textField.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    return YES;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease]; 
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (_editing-- == 1) {
        // 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }

    self.navigationItem.rightBarButtonItem = nil;

    // do something with the text here...

    return YES;
}

I have a table with 3 UITextFields added to the content views of cells (1 row per section). I have a 4th section that I insert so that I can scroll the particular field to be above the keyboard.

The problem I have (on the iPad only) is that when one of the text fields has firstResponder, it does not relinquish it when user taps on another field. Are there differences in the responder chain on an ipad? In the bellow code for my view controllers UITextFieldDelegate - textFieldShouldEndEditing does not get called when touching another field. Pressing the done button works as expected (unless another field has been touched).

Anything wrong with the code bellow?

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!_editing++) {
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }

    // scroll to section number in the text fields tag
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:textField.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    return YES;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease]; 
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (_editing-- == 1) {
        // 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }

    self.navigationItem.rightBarButtonItem = nil;

    // do something with the text here...

    return YES;
}

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

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

发布评论

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

评论(1

琉璃繁缕 2024-10-05 11:19:58

好的,我找到了一个令人满意的解决方法,似乎允许运行循环在请求动画删除部分之前执行,从而解决了问题。我猜这是苹果的错误?对于其他担心这个问题的人 - 我在 iPad 上运行 iOS3.2.1。

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (_editing == 1) {
        [self performSelector:@selector(hideFinalSection) withObject:nil afterDelay:0.0f];
    }
    else {
        _editing--;
    }

    self.navigationItem.rightBarButtonItem = nil;

        // do something with the text here...

    return YES;
}

- (void) hideFinalSection {
    if (!(--_editing)) { 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }
}

OK, i have found a satisfactory workaround, it seems allowing the run loop to execute before the request to animate removing a section fixes the problem. I guess this is an apple bug? For anyone else worried about this issue - i was running iOS3.2.1 on the iPad.

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (_editing == 1) {
        [self performSelector:@selector(hideFinalSection) withObject:nil afterDelay:0.0f];
    }
    else {
        _editing--;
    }

    self.navigationItem.rightBarButtonItem = nil;

        // do something with the text here...

    return YES;
}

- (void) hideFinalSection {
    if (!(--_editing)) { 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文