删除 iphone 普通键盘中的完成按钮
Posted
技术标签:
【中文标题】删除 iphone 普通键盘中的完成按钮【英文标题】:removing done button in normal keypad in iphone 【发布时间】:2012-01-03 10:18:37 【问题描述】:我有两个编辑文本一个编辑文本输入应该只接受数字,以便我有显示数字键盘。在数字键盘中没有完成按钮。所以现在我添加完成按钮using this link 现在完成按钮工作正常。我的问题是我的另一个编辑文本是普通键盘,键盘也会显示完成按钮。我想避免在那个特定的编辑文本中完成按钮。@987654322 @ 请指导我如何删除普通键盘中的那个按钮..
【问题讨论】:
【参考方案1】:if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
else
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
- (void)addButtonToKeyboard
// create custom button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0)
[doneButton setImage:[UIImage imageNamed:@"Btn-DoneDown-2.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"Btn-DoneUp-2.png"] forState:UIControlStateHighlighted];
else
[doneButton setImage:[UIImage imageNamed:@"Btn-DoneDown-1.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"Btn-DoneUp-1.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
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 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[keyboard addSubview:doneButton];
else
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
- (void)keyboardWillShow:(NSNotification *)note
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2)
[self addButtonToKeyboard];
- (void)keyboardDidShow:(NSNotification *)note
if(doneButton)
[doneButton removeFromSuperview];
doneButton = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
[self addButtonToKeyboard];
- (void)doneButton:(id)sender
[[NSNotificationCenter defaultCenter] removeObserver:self];
[doneButton removeFromSuperview];
doneButton = nil;
【讨论】:
最后只有我在搜索 [[NSNotificationCenter defaultCenter] removeObserver:self];非常感谢prem【参考方案2】:您可以使用以下代码将其删除:
注意:您必须将tag
分配给按钮。这里是300
//for remove done button from keyboard
- (void)removeButtonFromKeyboard
// locate keyboard view
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, remove the button
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
[[keyboard viewWithTag:300] removeFromSuperview];
else
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[[keyboard viewWithTag:300] removeFromSuperview];
isButtonAdded = NO ;
编辑
在- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
中,您可以通过文本字段的标签检查您是否想要完成按钮。喜欢:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
if (textField.tag == 1001)
// add button
// take a Boolean and make it true in your create button function
if(!doneButtonAdded)// if button is not added
// then add button
else if (textFiled.tag == 1002) // your normal key board
if(doneButtonAdded)// if button is added
// remove button
// in remove button function make doneButtonAdded to false
希望对你有帮助。
【讨论】:
以上是关于删除 iphone 普通键盘中的完成按钮的主要内容,如果未能解决你的问题,请参考以下文章
iPhone 键盘、完成按钮和 resignFirstResponder