iPhone 上的键盘和起床状态

发布于 2024-11-27 22:21:10 字数 148 浏览 3 评论 0原文

如何判断键盘是否已打开?

我有一个 UISearchbar 实例,它成为第一响应者。

当键盘出现时,通知会作为 API 的一部分发送出去,但我不想立即对此做出回应。我可以用布尔状态记录它,但这看起来很笨拙。我想知道是否有“吸气剂”,我可以打电话询问。

How do I find out if the keyboard is up?

I have a UISearchbar instance which becomes the first responder.

When the keyboard appears a notification is sent out as part of the API, however I don't want to respond to this right away. I could record this in a boolean state, but that seems clunky. I'd like to know if there is a "getter" some where I can call to find out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-12-04 22:21:10

我就是这样做的:

KeyboardStateListener.h

@interface KeyboardStateListener : NSObject {
    BOOL _isVisible;
}
+ (KeyboardStateListener *) sharedInstance;
@property (nonatomic, readonly, getter=isVisible) BOOL visible;
@end

KeyboardStateListener.m

#import "KeyboardStateListener.h"

static KeyboardStateListener *sharedObj;

@implementation KeyboardStateListener

+ (KeyboardStateListener *)sharedInstance
{
    return sharedObj;
}
+ (void)load
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    sharedObj = [[self alloc] init];
    [pool release];
}
- (BOOL)isVisible
{
    return _isVisible;
}
- (void)didShow
{
    _isVisible = YES;
}
- (void)didHide
{
    _isVisible = NO;
}
- (id)init
{
    if ((self = [super init])) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(didShow) name:UIKeyboardDidShowNotification object:nil];
        [center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end

然后用它来计算其余的:

KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
    //Keyboard is up
}

This is how I do it:

KeyboardStateListener.h

@interface KeyboardStateListener : NSObject {
    BOOL _isVisible;
}
+ (KeyboardStateListener *) sharedInstance;
@property (nonatomic, readonly, getter=isVisible) BOOL visible;
@end

KeyboardStateListener.m

#import "KeyboardStateListener.h"

static KeyboardStateListener *sharedObj;

@implementation KeyboardStateListener

+ (KeyboardStateListener *)sharedInstance
{
    return sharedObj;
}
+ (void)load
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    sharedObj = [[self alloc] init];
    [pool release];
}
- (BOOL)isVisible
{
    return _isVisible;
}
- (void)didShow
{
    _isVisible = YES;
}
- (void)didHide
{
    _isVisible = NO;
}
- (id)init
{
    if ((self = [super init])) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(didShow) name:UIKeyboardDidShowNotification object:nil];
        [center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end

Then use this to figure out the rest:

KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
    //Keyboard is up
}
情感失落者 2024-12-04 22:21:10

我能想到的唯一可靠的方法就是按照你说的去做。使用这样的通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];

然后

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

除此之外,您也许可以迭代视图子视图并查找键盘,例如:

UIView *keyboard = nil;

for (UIView *potentialKeyboard in [myWindow subviews]) {

    // iOS 4
    if ([[potentialKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
      potentialKeyboard = [[potentialKeyboard subviews] objectAtIndex:0];
    }                                                                                

    if ([[potentialKeyboard description] hasPrefix:@"<UIKeyboard"]) {
      keyboard = potentialKeyboard;
      break;
    }
  }

但我不确定当 SDK 更改时这是否会中断...

也许使用此方法并添加窗口的一个类别,以便您始终可以向窗口询问键盘......只是一个想法。

The only sure way that I can think to do it as you said. using notifications like this:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];

and then

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

Other than that, you may be able to iterate through your views subviews and look for the keyboard like:

UIView *keyboard = nil;

for (UIView *potentialKeyboard in [myWindow subviews]) {

    // iOS 4
    if ([[potentialKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
      potentialKeyboard = [[potentialKeyboard subviews] objectAtIndex:0];
    }                                                                                

    if ([[potentialKeyboard description] hasPrefix:@"<UIKeyboard"]) {
      keyboard = potentialKeyboard;
      break;
    }
  }

But I am not sure if this will break when the SDK changes ...

Maybe use this method and add a category to the window so that you can just always ask the window for the keyboard ... just a thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文