是否有任何通知发送键盘更改时(如从NumberPad到默认)
我想弄清楚如何在键盘改变时得到通知。 我想要做的是添加一个完成button键盘types4和5(NumberPad和PhonePad),一切工作正常,除了当我从一个TextField使用默认的KBtypes转换,通知KeyboardDidAppear不是被解雇。
这是我得到的:
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; }
然后,我为当前的KBtypes和正在编辑的当前TextField添加了一个Property:
#pragma mark - Delegate Methods -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { self.currentKBType = textField.keyboardType; self.curTextField = textField; return YES; }
然后我根据当前的KBtypes决定是否添加DONEbutton:
- (void)keyboardDidShow:(NSNotification *)note { if (self.currentKBType == 4 || self.currentKBType == 5) { [self addButtonToKeyboard]; } }
问题是通知在显示键盘时触发,而不是在更改时(从一个TextField转换到另一个指定不同KBtypes的另一个TextField)。
有什么build议么? 我错过了什么吗?
得出这个想法。 花了一点逻辑,但它的工作完美无瑕。 这是我做的:为以下内容添加私有属性:
@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签名的KeyboardDidShowNotification中:
- (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; }
最后,触摸完成button时调用的resignKeyboard方法:
-(void)resignKeyboard { [self.curTextField resignFirstResponder]; self.doneButtonDisplayed = NO; }
只要显示NumberPad或PhonePadtypes的键盘,就会添加“完成”button,并从其他键盘types中删除(仅当已添加时)。
经过testing,效果很好。