Objective c 键盘更改时(如从NumberPad更改为默认值)是否会发送任何通知
我正试图弄清楚,当键盘发生变化时,我如何得到通知。我想做的是在4和5型键盘(NumberPad和PhonePad)上添加一个“完成”按钮,一切正常,除了当我使用默认KB类型从文本字段转换时,键盘显示未被触发的通知 以下是我得到的:Objective c 键盘更改时(如从NumberPad更改为默认值)是否会发送任何通知,objective-c,ios,cocoa-touch,uikeyboard,uikeyboardtype,Objective C,Ios,Cocoa Touch,Uikeyboard,Uikeyboardtype,我正试图弄清楚,当键盘发生变化时,我如何得到通知。我想做的是在4和5型键盘(NumberPad和PhonePad)上添加一个“完成”按钮,一切正常,除了当我使用默认KB类型从文本字段转换时,键盘显示未被触发的通知 以下是我得到的: - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
然后,我为当前KB类型和正在编辑的当前文本字段添加了一个属性:
#pragma mark - Delegate Methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.currentKBType = textField.keyboardType;
self.curTextField = textField;
return YES;
}
然后,我根据当前KB类型决定是否添加“完成”按钮:
- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
[self addButtonToKeyboard];
}
}
问题是,当键盘显示时会触发通知,但当键盘更改(从一个文本字段转换到另一个指定不同KB类型的文本字段)时不会触发通知
有什么建议吗?我遗漏了什么吗?找到了答案。虽然有一点逻辑性,但效果完美。以下是我所做的: 为以下对象添加了私有属性:
@property (nonatomic) UIKeyboardType currentKBType;
@property(nonatomic,strong) UITextField *curTextField;
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic) BOOL doneButtonDisplayed;
然后在两个TextField委托方法中添加以下逻辑:
-(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注册的KeyboardDidShow通知中:
- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
if (!doneButtonDisplayed) {
[self addButtonToKeyboard];
}
} else {
if (doneButtonDisplayed) {
[self removeButtonFromKeyboard];
}
}
}
以及这些方法中引用的两种方法:
- (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;
}
这将在显示NumberPad或PhonePad类型键盘时添加“完成”按钮,并将其从其他键盘类型中删除(仅当已添加时)
经过测试,效果很好。代码运行正常。但当我第一次点击数字键盘时,就看不到“完成”按钮。您也可以这样做吗?
-(void)resignKeyboard {
[self.curTextField resignFirstResponder];
self.doneButtonDisplayed = NO;
}