如何从数字键盘中删除添加的完成按钮

时间:2012-10-19 07:41:22

标签: iphone keyboard numeric

这是我的代码......

`

- ( void ) registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
}

    // Called when the UIKeyboardDidShowNotification is sent.
- ( void ) keyboardWasShown:(NSNotification*)notification {
    [self addButtonToKeyboard];
}

#pragma -
#pragma Numeric keyboard done button

- ( void ) keyboardDoneClicked:(id)sender {
    [txtvannum resignFirstResponder];
    [txtmobnum resignFirstResponder];
//    [sender resignFirstResponder];
}


- ( void ) addButtonToKeyboard {
        // create custom button
//    if(keyButton){
    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:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    [doneButton addTarget:self action:@selector(keyboardDoneClicked:) forControlEvents:UIControlEventTouchUpInside];
        // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    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];
            }
        }

//    }
////    else{
////        return;}

}

`

我开发了一个应用程序,由用户输入他们的详细信息。其中有4个文本字段。其中两个需要数字类型的键盘。对于那个数字键盘我添加完成按钮在完成编辑后辞职。但是所有其他类型的键盘也完成了按钮。如何将完成的按钮添加到数字键盘,以便它应该隐藏所有其他类型。我正在努力。请帮助。

谢谢。

2 个答案:

答案 0 :(得分:0)

对于每个UITextField,使用可以获得值的函数setReturnKeyType:

typedef enum {
   UIReturnKeyDefault,
   UIReturnKeyGo,
   UIReturnKeyGoogle,
   UIReturnKeyJoin,
   UIReturnKeyNext,
   UIReturnKeyRoute,
   UIReturnKeySearch,
   UIReturnKeySend,
   UIReturnKeyYahoo,
   UIReturnKeyDone,
   UIReturnKeyEmergencyCall,
} UIReturnKeyType;

您必须分别为每个文本字段设置键类型。对于您不希望完成按钮的文本字段,只需将其设置为UIReturnKeyDefault。  希望这会有所帮助。

答案 1 :(得分:0)

首先,您必须设置通知以继续跟踪键盘的活动。要识别哪个UITextField处于活动状态,您必须为每个UITextField设置唯一标记。

在.h文件中&gt;&gt;

@interface myViewController : UIViewController
{
    UITextField *txtField1; // Allows Numeric input
    UITextField *txtField2; // Allows String input
    UITextField *txtField3; // Allows Numeric input
    UITextField *txtField4; // Allows String input
    UITextField *txtFieldTemp; // Temp textfield

    BOOL isReturned;
}

在.m文件中&gt;&gt;

- (void)viewDidLoad
{
    txtField1.tag = 0; 
    txtField2.tag = 1; 
    txtField3.tag = 2;
    txtField4.tag = 3; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIKeyboardWillChangeFrameNotification:) name:UIKeyboardDidShowNotification object:nil];
}

- (void)UIKeyboardWillChangeFrameNotification:(NSNotification *)note 
{
    if (txtFieldTemp.tag == 0 || txtFieldTemp.tag == 2)
    {
        [self addButtonToKeyboard];
    }
}

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 0 || textField.tag == 2)
    {
        [self addButtonToKeyboard];
    }
    else
    {
        [self removeButtonFromKeyboard];
    }
    txtFieldTemp = textField;
}
- (void)removeButtonFromKeyboard
{
   // Remove Keyboard logic
}