Ios iPad UI键盘隐藏/退出/取消按钮事件处理程序?
你能看到iPad键盘右下角的按钮吗 我想要访问该按钮的事件处理程序,因为,当按下该按钮时,我需要管理一些逻辑 我尝试的解决方案包括使用UIKeyboardWillHideNotification通知,如下所示:Ios iPad UI键盘隐藏/退出/取消按钮事件处理程序?,ios,ipad,uitextview,uisearchbar,uikeyboard,Ios,Ipad,Uitextview,Uisearchbar,Uikeyboard,你能看到iPad键盘右下角的按钮吗 我想要访问该按钮的事件处理程序,因为,当按下该按钮时,我需要管理一些逻辑 我尝试的解决方案包括使用UIKeyboardWillHideNotification通知,如下所示: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboardTapped:)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(hideKeyboardTapped:)
name:UIKeyboardWillHideNotification
object:nil];
然而,每次键盘被关闭时,包括按下搜索取消按钮、在背景上进行手势点击等,都会调用此功能;所有这些都有稍微不同的逻辑
所以我只需要有一套逻辑,当特定的“隐藏键盘”按钮被按下时
ui键盘由ui搜索栏激活(适用于需要额外信息的用户)
任何帮助都将不胜感激;我会回答你可能有的任何问题。没有一个官方的、苹果认可的方法来做到这一点
如果你不介意随意使用私有类,可能会让你被应用商店拒绝,那么有一种不受支持的方法。基本上,您可以监听键盘的activeKey
属性的更改,如果新的活动键的名称为“dismise key”
,则按下dismise键:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"activeKey"])
{
id activeKey = [object valueForKey:keyPath];
NSString *keyName = [activeKey valueForKey:@"name"];
if ([keyName isEqualToString:@"Dismiss-Key"])
{
[self didTapDismissKey];
}
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Search for the UIKeyboardLayoutStar class, which seems to manage layout for the keyboard, and observe its activeKey property
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
[self traverseViewHierarchyWithView:topWindow block:^(UIView *view, BOOL *stop) {
if ([view isKindOfClass:NSClassFromString(@"UIKeyboardLayoutStar")])
{
[view addObserver:self forKeyPath:@"activeKey" options:0 context:NULL];
}
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
// Don't forget to remove the controller as an observer!
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
[self traverseViewHierarchyWithView:topWindow block:^(UIView *view, BOOL *stop) {
if ([view isKindOfClass:NSClassFromString(@"UIKeyboardLayoutStar")])
{
[view removeObserver:self forKeyPath:@"activeKey"];
}
}];
}
- (BOOL)traverseViewHierarchyWithView:(UIView *)view block:(void (^)(UIView *, BOOL *))block
{
BOOL stop = NO;
block(view, &stop);
if (stop)
{
return YES;
}
for (UIView *subview in view.subviews)
{
if ([self traverseViewHierarchyWithView:subview block:block])
{
return YES;
}
}
return NO;
}
- (void)didTapDismissKey
{
NSLog(@"Dismiss key tapped");
}
在iOS 7 iPad模拟器上测试,可以正常工作。我不知道你是否可以将监听器放在键盘的一个按钮上,但你可以尝试在视图上设置一些布尔值,可以打开/关闭你想要或不想要处理的事件(我会这样处理),例如:
在您创建的类中添加自定义通知观察者/侦听器
要执行事件的方法
在将触发通知的控制器上添加Apple默认键盘隐藏侦听器
然后,在可以触发事件的视图上。。。设置布尔值以激活/停用事件操作
@另一类
-(无效)视图将显示:(BOOL)动画{
}
-(无效)视图将消失:(BOOL)已设置动画{
}
@触发类
-(void)viewWillAppear:(BOOL)animated{
self.isNotificationNeeded = NO;
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(onKeyboardHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
-(void)onKeyboardHide:(id)sender{
//When your event is Happening
if (self.isNotificationNeeded == YES) {
NSNotification *n = [NSNotification notificationWithName:@"doSomethingNotification" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:n];
self.isNotificationNeeded = NO;
}
}
-(void)userClickedCancelButton{
//If this is a keyboard hide event that you don't want to trigger
self.isNotificationNeeded = NO;
}
//SearchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
self.isNotificationNeeded = YES; //If your event can happen from here
return YES;
}
我不知道这是可能的。
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"doSomethingNotification" object:nil];
-(void)viewWillAppear:(BOOL)animated{
self.isNotificationNeeded = NO;
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(onKeyboardHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
-(void)onKeyboardHide:(id)sender{
//When your event is Happening
if (self.isNotificationNeeded == YES) {
NSNotification *n = [NSNotification notificationWithName:@"doSomethingNotification" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:n];
self.isNotificationNeeded = NO;
}
}
-(void)userClickedCancelButton{
//If this is a keyboard hide event that you don't want to trigger
self.isNotificationNeeded = NO;
}
//SearchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
self.isNotificationNeeded = YES; //If your event can happen from here
return YES;
}