如何检查硬件键盘是否连接?

时间:2015-08-20 08:07:57

标签: ios objective-c iphone

我有3个文本字段。我根据文本字段的位置为视图创建了一个动态动画,当我检查“连接硬件键盘”时,视图使动画区域变黑。

有人能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以注册4个通知。这是一个例子:

#pragma mark - Lifecycle

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidAppear:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Notifications

- (void)keyboardWillAppear:(NSNotification *)note
{

}

// E.t.c

这些通知的好处是,只有在切换虚拟键盘时才会触发它们。因此,您可以使用这些通知触发视图切换。当用户连接硬件键盘时,不会发生任何事情。