我在我的应用程序中创建了许多文本字段,我希望它们在用户完成编辑时关闭键盘。我知道如果 textField 是由 IB 创建的,并且有一个 IBAction 我可以使用但是如果 textField 是这样创建的,
int top = 60;
for(NSUInteger i=0; i< [kArray count]; i++){
UITextField *kField = [kArray objectAtIndex:i];
kField.frame = CGRectMake(130, top, 60, 30);
kField.borderStyle = UITextBorderStyleRoundedRect;
kField.autocorrectionType = UITextAutocorrectionTypeNo;
kField.returnKeyType = UIReturnKeyDone;
kField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[scrollView addSubview:kField];
top += 50;
}
如何在用户按下完成按钮时关闭键盘?
最佳答案
使用 UITextFieldDelegate 方法:
#pragma mark UITextFieldDelegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
记得让你的类像这样遵守委托(delegate):
@interface MyViewController : UIViewController <UITextFieldDelegate>
关于iphone - 以编程方式触发 didEndOnExit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8842637/