iOS:" hide"的监听者键盘上的按钮

时间:2017-08-24 13:10:10

标签: ios objective-c

我正在使用

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField

当用户点击返回或完成按钮时删除键盘,它完美地工作。我的问题是,当我的应用程序处于横向模式或我在iPad中运行它时,"隐藏"按钮被添加到键盘中(图中显示的按钮)。当我单击它时,键盘被隐藏但从未调用textFieldShouldReturn

如何点击此按钮时如何检测?

enter image description here

2 个答案:

答案 0 :(得分:1)

要检测来自UITextField的键盘何时出现,我们可以在viewDidLoad中设置观察者,例如:

- (void)viewDidLoad {
[super viewDidLoad];

// setup keyboard observers
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardCameUp:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWentAway:) name:UIKeyboardWillHideNotification object:nil];
}

这些观察者将在我们的类中调用一个方法(使用@selector)。我叫做keyboardCameUp和keyboardWentAway:

- (void)keyboardCameUp:(NSNotification *)notification {
NSLog(@"Keyboard came up!");
}

- (void)keyboardWentAway:(NSNotification *)notification {
NSLog(@"Keyboard went away!");
}

src:例如http://pinkstone.co.uk

答案 1 :(得分:1)

接收键盘通知 当键盘显示或隐藏时,iOS会向任何已注册的观察者发送以下通知:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification

您可以从Apple document

获取详细信息

例如

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardhideHandle:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];



- (void) keyboardhideHandle:(NSNotification *)notification {
    NSLog(@"you received the action here");
}