iPad UIKeyboard隐藏/退出/关闭按钮事件处理程序?

时间:2014-03-19 22:44:12

标签: ios ipad uitextview uisearchbar uikeyboard

你能看到iPad键盘右下角的按钮吗?

iPad UIKeyboard

我想要访问该按钮的事件处理程序,因为,当我按下特定按钮时,我需要管理一些逻辑。

我尝试过的解决方案包括使用UIKeyboardWillHideNotification通知,如下所示:

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

然而,这被称为每次键盘被解除,包括按下搜索取消按钮,手势点击背景等;所有这些逻辑略有不同。

所以我只需要在按下特定的“隐藏键盘”按钮时有一套逻辑。

UIKeyboard由UISearchBar激活(对于那些需要额外信息的人)。

非常感谢任何帮助;我会回答你的任何问题。

2 个答案:

答案 0 :(得分:1)

没有官方的,苹果认可的方式来做到这一点。

如果您不介意使用私人课程,可能会让您从App Store拒绝,则会有一种不受支持的方法。基本上,您会听取键盘activeKey属性的更改,如果新的活动键名称为"Dismiss-Key",则会点击关闭键:

- (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模拟器上测试,它可以正常工作。

答案 1 :(得分:0)

我不知道你是否可以把听众放在键盘的一个按钮上,但你可以尝试在视图上设置一些布尔值,这可以打开/关闭你想要的事件或者不要# 39;我想处理(我会这样做),例如:

  1. 在您所在的班级添加自定义通知观察者/听众 想要执行事件的方法。

  2. 在将触发通知的控制器上添加Apple默认键盘隐藏侦听器。

  3. 然后,在可以触发事件的视图上...设置布尔值以激活/停用事件动作

    @Some Other Class

  4. - (无效)viewWillAppear中:(BOOL)动画{

    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(doSomething)
                                                name:@"doSomethingNotification"
                                              object:nil];
    

    }

    - (无效)viewWillDisappear:(BOOL)动画{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"doSomethingNotification" object:nil];
    

    }

    @Trigger Class

    -(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;
    
    
        }