我有两个编辑文本,一个编辑文本输入应该只接受数字,以便我有显示数字键盘。数字键盘没有完成按钮。所以现在我添加完成按钮using this link现在完成按钮工作正常。我的问题是我的另一个编辑文本是正常键盘,键盘也完成按钮将显示。我想避免在该特定编辑文本中完成按钮。 请指导我如何在普通键盘中删除该按钮..
答案 0 :(得分:2)
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;
}
答案 1 :(得分:1)
您可以使用以下代码将其删除:
注意:您必须将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
}
}
}
希望它对你有所帮助。