Is there any notification that gets sent when the Keyboard changes (like from NumberPad to Default)

南笙酒味 提交于 2019-12-10 11:25:19

问题


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.

Here is what I got:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

}

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

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

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?


回答1:


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;

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

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

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

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

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.

Tested and works great.



来源:https://stackoverflow.com/questions/9897425/is-there-any-notification-that-gets-sent-when-the-keyboard-changes-like-from-nu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!