我在我的应用程序中创建了很多文本字段,我希望它们在用户完成编辑时关闭键盘。我知道如果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;
}
当用户按下完成按钮时,如何关闭键盘?
答案 0 :(得分:2)
使用UITextFieldDelegate方法:
#pragma mark UITextFieldDelegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
请记住让您的班级遵守代表:
@interface MyViewController : UIViewController <UITextFieldDelegate>
答案 1 :(得分:0)
试试这个:
- (BOOL)textFieldShouldReturn: (UITextField *)textField {
[kField resignFirstResponder];
return YES;
}