3

这是我的代码....

`

- ( 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 个文本字段。其中两个需要数字类型的键盘。对于那个数字键盘,我添加了完成按钮以在完成编辑后退出。但是完成按钮是也适用于所有其他类型的键盘。如何将完成按钮添加到数字键盘,以便它应该隐藏所有其他类型。我在这方面很挣扎。请帮忙。

谢谢你。

4

2 回答 2

0

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

在 .h 文件中 >>

@interface myViewController : UIViewController
{
    UITextField *txtField1; // 允许数字输入
    UITextField *txtField2; // 允许字符串输入
    UITextField *txtField3; // 允许数字输入
    UITextField *txtField4; // 允许字符串输入
    UITextField *txtFieldTemp; // 临时文本字段

    BOOL 已返回;
}

在 .m 文件中 >>

- (void)viewDidLoad
{
    txtField1.tag = 0;
    txtField2.tag = 1;
    txtField3.tag = 2;
    txtField4.tag = 3;
    [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@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];
    }
    别的
    {
        [self removeButtonFromKeyboard];
    }
    txtFieldTemp = 文本字段;
}
- (void)removeButtonFromKeyboard
{
   // 移除键盘逻辑
}
于 2012-10-19T09:16:59.080 回答
0

对于每个UITextField使用setReturnKeyType:可以获取值的功能,

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

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

于 2012-10-19T07:58:39.663 回答