我正在创建如下所示的 iPhone 应用程序。
在屏幕的末尾我有文本字段。我为此添加了一个代表。因为它是数字键盘,所以我单独添加了按钮,以便在单击按钮时隐藏键盘。
下面是我的代码:
.h
@interface SearchViewController : UIViewController<UITextFieldDelegate>
@property (retain, nonatomic) IBOutlet UITextField *textField006;
@property (retain, nonatomic) IBOutlet UIButton *doneButton;
- (IBAction)doneAction:(id)sender;
.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"textFieldShouldReturn");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"textFieldDidBeginEditing");
// Ensure the relevant text field is visible
CGAffineTransform translation = CGAffineTransformIdentity;
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenHeight = screenSize.height;
if (screenHeight==480 || screenHeight==568) {
translation = CGAffineTransformMakeTranslation(0, -120);
doneButton.hidden = NO;
NSLog(@"line 3");
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
}
}
- (IBAction)doneAction:(id)sender {
doneButton.hidden = NO;
doneButton.hidden = YES;
[textField006 resignFirstResponder];
[UIView beginAnimations:nil context:nil];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
[self.textField006 resignFirstResponder];
}
为什么键盘不隐藏?我怎样才能隐藏它?
Keyboard == Decimal Pad Return key >> Go Auto-enable Return key = Ticked
最佳答案
一定要用endEditing:如果它当前没有正确隐藏。
关于endEditing
:
"endEditing causes the view (or one of its embedded text fields) to resign the first responder status."
"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."
因此,以下应该有效(在您的按钮点击操作方法内):
[self.view endEditing:YES];
关于ios - 单击按钮时隐藏键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15521508/