【发布时间】:2010-10-22 10:50:16
【问题描述】:
我有一个表格,其中添加了 3 个 UITextFields 到单元格的内容视图(每节 1 行)。我插入了第 4 部分,以便将特定字段滚动到键盘上方。
我遇到的问题(仅在 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;
}
【问题讨论】:
-
我会写 (!(--_editing)) 以保持一致性,但除此之外,对于在 iPhone 上工作的代码,...
-
是的,它的奇怪..app 也是只为 iphone 界面开发的,所以 ipad 处于“iphone 模式”。通过强制退出 textFieldShouldBeginEditing 中的当前第一响应者来管理解决方法...但这并不完美(在切换字段时键盘不会保持不动 - 需要按两次该字段才能更改)
标签: iphone ipad uitableview uitextfield uiresponder