键盘更改(例如从NumberPad更改为Default)时是否发送任何通知? [英] Is there any notification that gets sent when the Keyboard changes (like from NumberPad to Default)

查看:141
本文介绍了键盘更改(例如从NumberPad更改为Default)时是否发送任何通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在键盘改变时得到通知.我想做的是将DONE按钮添加到4&类型的键盘上. 5(NumberPad和PhonePad),一切正常,除了当我使用默认KB类型从TextField过渡时,没有触发KeyboardDidAppear的通知.

I am trying to figure out how I can get notified when the keyboard changes. What I am trying to do is add a DONE button to keyboard of type 4 & 5 (NumberPad and PhonePad), everything is working fine, except when I transition from a TextField using a Default KB type, The notification that the KeyboardDidAppear isn't being fired.

这就是我得到的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification 
                                               object:nil]; 

}

然后,我为当前的KB类型和正在编辑的当前TextField添加了一个属性:

Then I added a Property for the current KB type and the current TextField being edited:

#pragma mark - Delegate Methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.currentKBType = textField.keyboardType;
    self.curTextField = textField;
    return YES;
}

然后我根据当前的KB类型决定是否添加该DONE按钮:

I then make a decision on whether or not to add that DONE button based on the current KB type:

- (void)keyboardDidShow:(NSNotification *)note {
    if (self.currentKBType == 4 || self.currentKBType == 5) {
        [self addButtonToKeyboard];
    }
}

问题在于,显示键盘时会触发通知,但更改时不会触发(从一个TextField过渡到指定另一个KB类型的另一个TextField.

Problem is that the notification fires when the keyboard is displayed, but not when it changes (transitions from one TextField to another that specifies a different KB type.

有什么建议吗?我想念什么吗?

Any suggestions? Am I missing something?

推荐答案

知道了这一点.采取了一些逻辑,但它可以完美地工作.这是我所做的: 为以下项添加了私有属性:

Got this figured out. Took a little logic, but it works flawlessly. Here is what I did: Added private properties for:

@property (nonatomic) UIKeyboardType currentKBType;
@property(nonatomic,strong) UITextField *curTextField;
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic) BOOL doneButtonDisplayed;

然后在两个TextField委托方法中添加了以下逻辑:

Then added the following logic in the both the TextField delegate method:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.currentKBType = textField.keyboardType;
    if (textField.keyboardType == 4 || textField.keyboardType == 5) {
        if (!doneButtonDisplayed) {
            [self addButtonToKeyboard];
        }
    } else {
        if (doneButtonDisplayed) {
            [self removeButtonFromKeyboard];
        }
    }

    self.curTextField = textField;
    return YES;
}

然后在我在viewDidLoad中为VC签名的KeyboardDidShowNotification中:

And in the KeyboardDidShowNotification that I signed the VC up for in the viewDidLoad:

- (void)keyboardDidShow:(NSNotification *)note {
    if (self.currentKBType == 4 || self.currentKBType == 5) {
        if (!doneButtonDisplayed) {
            [self addButtonToKeyboard];
        }
    } else {
        if (doneButtonDisplayed) {
            [self removeButtonFromKeyboard];
        }
    }
}

以及这些方法中引用的两种方法:

And the two methods referenced in these methods:

- (void)addButtonToKeyboard {

    // create custom button
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;

    [doneButton setImage:[UIImage imageNamed:@"DoneNormal.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneHL.png"] forState:UIControlStateHighlighted];

    [doneButton addTarget:self action:@selector(resignKeyboard) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    if ([[[UIApplication sharedApplication] windows] count] > 1) {
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
        UIView* keyboard;
        for(int i=0; i<[tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            // keyboard found, add the button
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) {
                [keyboard addSubview:doneButton];
                self.doneButtonDisplayed = YES;
            }
        }

    }
}

- (void)removeButtonFromKeyboard {  
    [doneButton removeFromSuperview];
    self.doneButtonDisplayed = NO;
}

最后,每当触摸完成"按钮时就会调用resignKeyboard方法:

And finally, the resignKeyboard method that is called whenever the Done button is touched:

-(void)resignKeyboard {
    [self.curTextField resignFirstResponder];
    self.doneButtonDisplayed = NO;
}

这将在显示NumberPad或PhonePad类型的键盘时添加该完成"按钮,并将其(仅在已添加时)从其他键盘类型中删除.

This will add that Done button whenever a NumberPad or PhonePad type keyboard is displayed and remove it (only when it has been added) from the other keyboard types.

经过测试,效果很好.

这篇关于键盘更改(例如从NumberPad更改为Default)时是否发送任何通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆