2012-10-19 101 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 
}