【问题标题】:How to remove added done button from numeric keyboard如何从数字键盘中删除添加的完成按钮
【发布时间】:2012-10-09 19:01:01
【问题描述】:

这是我的代码....

`

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

谢谢。

【问题讨论】:

  • 我已经发布了我在我的应用程序中使用的代码。请帮助

标签: iphone keyboard numeric


【解决方案1】:

对于每个UITextField,使用函数setReturnKeyType:可以取值,

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

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

【讨论】:

  • 但是当我们为数字小键盘设置完成返回键时,默认情况下它没有分配。所以我们需要添加我们自己的自定义按钮。我什至添加了完成按钮,并且它工作正常.但我的问题是,这个完成按钮适用于该视图中的所有类型的键盘。如何为那些使用字母键盘的文本字段隐藏或删除该 btn。
  • 我看过你的代码。我们可以使用UITextField- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField 代表。以便您可以找到即将带键盘的文本字段。如果文本字段是数字,则在此处设置一个标志。您的 keyboardWasShown: 函数肯定会在文本字段委托之后调用。如果设置了标志,您可以添加按钮,否则您可以删除按钮。
【解决方案2】:

首先,您必须设置一个通知来持续跟踪键盘的活动。要识别哪个 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 { // 移除键盘逻辑 }

【讨论】:

    猜你喜欢
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2023-03-15
    • 2011-05-20
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多