我正在开发一个应用程序,它的每个单元格的右侧都有一个带有textField的tableView (有20多个单元格)。我已经为除最后一行之外的每一行创建了自定义单元格。最后一行只有一个按钮。
现在,我想在单击按钮时调用resignFirstResponder。
我该怎么做请帮忙?
发布于 2011-11-25 22:02:54
您必须跟踪哪个文本字段在哪个单元格中有第一个响应器,并像这样重新签署它。
[myCellTextField resignFirstResponder];发布于 2011-11-25 22:11:01
您可能希望使用键盘跟踪文本字段。在控制器中实现<UITextFieldDelegate>协议,并将控制器设置为每个文本字段的委托。像这样编写textFieldDidBeginEditing:方法,设置一个名为currentTextField的实例变量
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = [textField retain];
}然后,在按钮的操作中运行[currentTextField resignFirstResponder]。
发布于 2011-11-26 00:56:03
Aopsfan的答案可能是到目前为止最好的解决方案。但是,要添加该对象(因为我无法发表评论),请记住释放该对象:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (currentTextField != nil) {
[currentTextField release];
}
currentTextField = [textField retain];
}最好还是使用@property和@synthesize,这样运行时就可以为你做内存管理了。
ViewController.h
@property (nonatomic, retain) UITextField* currentTextField;ViewController.m
@synthesize currentTextField = _currentTextField;
- (void)viewDidLoad|Appear {
self.currentTextField = nil;
}
- (void) dealloc {
[_currentTextField release], _currentTextField = nil;
...
[super dealloc];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentTextField = textField;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.currentTextField) {
[self.currentTextField resignFirstResponder];
}
}https://stackoverflow.com/questions/8269912
复制相似问题