2012-10-19 60 views
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个文本字段。其中两个需要数字键盘。对于那个数字键盘,我在完成编辑后添加了完成按钮以辞职。但是完成按钮也适用于所有其他类型的键盘。如何添加完成按钮oly数字键盘,以便它应该隐藏所有其他呃type.I在这挣扎。请帮助。

谢谢。

+0

发布您的代码,请。 – Frank

+0

我已经发布我的代码,我用我的app.Pls帮助 – NSUserDefault

回答

0

对于每个UITextField使用功能setReturnKeyType:可实现值,

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

而且你必须设置separately.For文本字段每个文本字段键类型,你不想完成按钮只需将其设置为UIReturnKeyDefault即可。 希望这有助于。

+0

但是,当我们为数字小键盘设置完成返回键,它不是默认分配。所以我们需要添加我们自己的自定义按钮。我甚至补充说完成按钮,也工作正常。但我的问题是,这个完成按钮是在该视图中的所有类型的键盘。如何隐藏或删除那些使用字母键盘的文本字段的btn。 – NSUserDefault

+0

我曾见过你的代码。我们可以使用' - (BOOL)textFieldShouldBeginEditing:(UITextField *)'UITextField'的textField委托。这样你就可以找到哪个文本框即将带上键盘。如果文本字段是数字,请在此处设置一个标志。您的'keyboardWasShown:'函数将在文本字段委托之后调用。所以你可以添加按钮,如果标志设置,否则你可以删除按钮。 –

0

首先,您必须设置一个通知来跟踪键盘的活动。为了确定哪个UITextField处于活动状态,您必须为每个标签设置一个唯一的标签。

在.h文件中>>

 
@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文件>>

 
- (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 
}