UIKeyboardWillHide没有被触发
我在这里阅读了很多关于这个主题的文章,但是我没能find我的问题的答案,所以,希望你不会对另一个UIKeyboard文章感到厌烦:-)
在我的视图控制器的实现中,我添加了self
作为观察者的两个通知UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
,传递select器keyboardWillShow:
和keyboardWillHide:
处理通知。 当我触摸一个UITextField
, keyboardWillShow:
方法被调用,但是当我按下“Done”button(这将closures键盘)时,不会调用keyboardWillHide:
方法。
真的,我想让我的UITextField
显示一个键盘右下方的“隐藏button”,但我找不到正确的键盘types。 也许我需要将textfield retuntype设置为“…完成”。 这样我看到“返回”键变成了“完成”。
所以我设置了一个工具栏作为我的UITextField
的inputAccessoryView
,所以现在我可以使用“完成”button来显示上面的工具栏的标准键盘。 当用户触摸那个button时,我用resignFirstResponder
方法隐藏键盘。
奇怪的是,当我呼吁resignFirstResponder
, UIKeyboardWillHideNotification
不张贴; 至less不调用keyboardWillHide:
方法。
你对我有什么build议? 我真的想显示一个键盘与向下箭头的小button隐藏键盘,但也是这个解决scheme可能是正确的,但我想调整视图,并做到这一点,我需要观察员UIKeyboardWillHideNotification
。
非常感谢你的帮助…
(添加:)
在viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:[[self view] window]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:[[self view] window]];
我把这些声明从“你的”之一发布:-)但willShow的作品…
UIToolbar
中被指定为我的文本字段的inputAccessoryView
的“完成”button的操作是:
-(void)keyboardDone { [msgTextField resignFirstResponder];
closures:OK! 当一个开发者是愚蠢的…这是愚蠢的:-) 🙂
这是我更正的willHide方法:
-(void)keyboardWillHide:(NSNotification*)n { NSDictionary* userInfo; CGSize keyboardSize; CGRect viewFrame; /* This was the bad guy :) I forgot to delete it * after I previously copied the willShow method that * checks if keyboard is already shown (if so returns). * * if( keyboardIsShown ) * return; */ userInfo = [n userInfo]; keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; viewFrame = [[self scrollView] frame]; viewFrame.size.height += ( keyboardSize.height - TABBAR_HEIGHT ); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.5]; [[self scrollView] setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = NO; NSLog(@"HIDE\n"); }
首先我要感谢大家帮助我做这些无用的工作。 我想给你一些观点,所以我会尝试为每个答案提出一个“兴趣点”,但是我需要select正确的一个…很难的部分… 🙂
再次抱歉…我真的没有看到if()语句…
如果你阅读UIWindow
的文档,它说这些通知的通知对象是nil
。 您将addObserver:selector:name:object:
作为对象传递给addObserver:selector:name:object:
method。 尝试通过nil
而不是:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
检查,如果keyboardDone
真的被调用(即使用NSLog(@"%@", @"keyboard done called");
)。 如果它被调用,但resignFirstResponder不帮助解雇键盘,那么试试这个:
[self.view endEditing:YES];
还请提供您的keyboardWillHide:
方法。
要设置键盘使其具有“完成”button,请执行以下操作:
1)设置您的视图控制器,以便它实现UITextFieldDelegate。 例如:
#import <UIKit/UIKit.h> @interface TX_ViewController : UIViewController <UITextFieldDelegate> @property (nonatomic, retain) IBOutlet UITextField *textField; @end
2)在你的视图控制器实现文件中,使用下面的代码来设置键盘:
- (void)viewDidLoad { [self.textField setDelegate:self]; [self.textField setReturnKeyType:UIReturnKeyDone]; [self.textField addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }
3)如果你想在按下DONEbutton的时候做一些事情,只需将下面的函数添加到你的视图控制器的实现文件中:
- (IBAction)textFieldFinished:(id)sender { [sender resignFirstResponder]; }
另外,如果您使用“接口”构build器来创build接口,请不要忘记设置TextField的IBOutlet引用; 否则,你的class级将不会收到来自XIB的消息。
我在一个示例应用程序中进行设置,只是为了查看它是否有效,它是否以您希望应用程序执行的方式执行。
需要注意的是,当用户通过隐藏button隐藏软键盘时,不会调用隐藏方法。 显示方法再次被调用,但除了home行工具栏外,键盘几乎不在屏幕上。