LJ *_*son 2 cocoa-touch objective-c uikeyboard ios uikeyboardtype
我试图找出当键盘发生变化时如何得到通知.我想要做的是为类型4和5的键盘(NumberPad和PhonePad)添加一个DONE按钮,一切正常,除非我使用默认KB类型从TextField转换,KeyboardDidAppear不是通知被解雇了
这是我得到的:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
Run Code Online (Sandbox Code Playgroud)
然后我为当前的KB类型和正在编辑的当前TextField添加了一个Property:
#pragma mark - Delegate Methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.currentKBType = textField.keyboardType;
self.curTextField = textField;
return YES;
}
Run Code Online (Sandbox Code Playgroud)
然后,我根据当前的KB类型决定是否添加DONE按钮:
- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
[self addButtonToKeyboard];
}
}
Run Code Online (Sandbox Code Playgroud)
问题是在显示键盘时会触发通知,但在更改时不会触发(从一个TextField转换到指定不同KB类型的另一个TextField).
有什么建议?我错过了什么吗?
得到了这个想法.采取了一点逻辑,但它完美无瑕.这是我做的:添加私有属性:
@property (nonatomic) UIKeyboardType currentKBType;
@property(nonatomic,strong) UITextField *curTextField;
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic) BOOL doneButtonDisplayed;
Run Code Online (Sandbox Code Playgroud)
然后在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;
}
Run Code Online (Sandbox Code Playgroud)
在我在viewDidLoad中签署了VC的KeyboardDidShowNotification中:
- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
if (!doneButtonDisplayed) {
[self addButtonToKeyboard];
}
} else {
if (doneButtonDisplayed) {
[self removeButtonFromKeyboard];
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且这些方法中引用了两种方法:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
最后,只要触摸完成按钮,就会调用resignKeyboard方法:
-(void)resignKeyboard {
[self.curTextField resignFirstResponder];
self.doneButtonDisplayed = NO;
}
Run Code Online (Sandbox Code Playgroud)
这将在显示NumberPad或PhonePad类型键盘时添加Done按钮,并从其他键盘类型中删除它(仅在添加时).
经过测试,效果很好.
| 归档时间: |
|
| 查看次数: |
1666 次 |
| 最近记录: |