2011-11-23 24 views
2

我正在開發iPhone應用程序。我在其中一個視圖中有四個文本框。在這四個文本框中,我有一個文本框,其中將使用數字鍵盤。但在數字鍵盤上沒有返回鍵。所以我通過使用以下代碼以編程方式添加了「完成」按鈕。完成按鈕僅用於數字鍵盤

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
    [[NSNotificationCenter defaultCenter] addObserver:self     
      selector:@selector(keyboardDidShow:) 
               name:UIKeyboardDidShowNotification object:nil];   
} else { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification object:nil]; 
} 

    - (void)addButtonToKeyboard { 
// create custom button 
UIButton *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(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 
// locate keyboard view 
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
UIView* keyboard; 
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]; 
     } 
    } 
    } 

    - (void)keyboardWillShow:(NSNotification *)note { 
// if clause is just an additional precaution, you could also dismiss it 
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) { 

     [self addButtonToKeyboard]; 


} 
    } 

     - (void)keyboardDidShow:(NSNotification *)note { 
// if clause is just an additional precaution, you could also dismiss it 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 

     [self addButtonToKeyboard]; 


     } 
    } 


- (void)doneButton:(id)sender { 
    NSLog(@"doneButton"); 
    NSLog(@"Input: %@", contactno.text); 

    [contactno resignFirstResponder]; 

    } 

對於這個特定的文本字段很好。但這個按鈕也是爲其他文本框添加的。如何取消隱藏其他文本框的按鈕。 enter image description here

非常感謝。

+0

http://cocoacontrols.com/platforms/ios/controls/amkeyboardnumberpad看看這個實施,對我來說效果很好 –

+0

這是一件非常可怕的事情。如果鍵盤的實現發生變化,它很容易中斷。它在iPad上無法使用,並且它尤其不適用於iOS 5中的分離鍵盤。 –

回答

23

請請請請請請請請請請請請請不要這樣做。這太可笑了。考慮到這一點:

  • 如果數字鍵盤的佈局改變怎麼辦?
  • 如果按鍵的顏色改變了怎麼辦?
  • 如果鍵盤的實現發生變化,使其不再是索引1處的窗口,該怎麼辦?
  • 如果鍵盤和外圍設備主機的實現發生了改變,那麼內省描述就會中斷嗎?
  • 如果您在iPad上運行此鍵盤的佈局完全不同,該怎麼辦?

這些只是將您的用戶界面升級到私有視圖層次結構時遇到的各種各樣的問題。

不要這樣做。

這裏有2層的替代品來代替:

  1. 使用的-inputAccessoryView物業每UIResponder(因此每UIView)來定義一個 「完成」 按鈕UIToolbar。當鍵盤進入和退出時,inputAccessoryView將位於鍵盤上方。

  2. 添加透明UIView在捕獲點擊事件,並導致整個UI鍵盤駁回

+15

3.提交一個錯誤報告,要求Apple將令人毛骨悚然的Done按鈕添加到數字鍵盤。 –

+1

@JoshCaswell這是一個給定;)http://filearadar.com/ –

+1

那麼客戶的期望呢?這也很重要! – Maulik

1

我想你可以保留一個全局變量UITextField * selectedTextField作爲用戶當前選擇的textField的指針(弱引用)。還將IBOutlets存儲在您的視圖中的所有UITextField對象(強引用)。然後包裹整個身體addButtonToKeyboard大,如果條款中:

if (self.selectedTextField == self.contactNumberTextField){ 
    // ... add button 
} 

你需要建立UITextField委託方法

- (void)textFieldDidBeginEditing:(UITextField *)textField; 

知道哪些文本框是當前選擇之一。

事實上,我認爲你甚至可以在沒有網點的情況下使用selectedTextField.keyboardType屬性,但是再一次,你可能已經有了網點,因爲你需要它們來讀取用戶輸入。

希望這會有所幫助!

1

把這段代碼放在你的項目中,並設置iboutlet的文本字段。它也有一些未使用的方法,但你可以使用它來選擇完成按鈕。然後你將刪除未使用的方法。這僅用於Get Done按鈕數字鍵盤鍵盤上

.H文件

@interface aaaViewController : UIViewController 
{ 
IBOutlet UITextField *txthello; 
UIImage *numberPadDoneImageNormal; 
UIImage *numberPadDoneImageHighlighted; 
UIButton *numberPadDoneButton; 

} 

@property (nonatomic, retain) UIImage *numberPadDoneImageNormal; 
@property (nonatomic, retain) UIImage *numberPadDoneImageHighlighted; 
@property (nonatomic, retain) UIButton *numberPadDoneButton; 

.m文件

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

// Add listener for keyboard display events 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardDidShow:) 
               name:UIKeyboardDidShowNotification 
               object:nil];  
} else { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 
} 

// Add listener for all text fields starting to be edited 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(textFieldDidBeginEditing:) 
              name:UITextFieldTextDidBeginEditingNotification 
              object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardDidShowNotification 
                object:nil];  
} else { 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillShowNotification 
                object:nil]; 
} 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:UITextFieldTextDidBeginEditingNotification 
               object:nil]; 
[super viewWillDisappear:animated]; 
} 

- (UIView *)findFirstResponderUnder:(UIView *)root { 
if (root.isFirstResponder) 
    return root;  
for (UIView *subView in root.subviews) { 
    UIView *firstResponder = [self findFirstResponderUnder:subView];   
    if (firstResponder != nil) 
     return firstResponder; 
} 
return nil; 
} 

- (UITextField *)findFirstResponderTextField { 
UIResponder *firstResponder = [self findFirstResponderUnder:[self.view window]]; 
if (![firstResponder isKindOfClass:[UITextField class]]) 
    return nil; 
return (UITextField *)firstResponder; 
} 

- (void)updateKeyboardButtonFor:(UITextField *)textField { 

// Remove any previous button 
[self.numberPadDoneButton removeFromSuperview]; 
self.numberPadDoneButton = nil; 

// Does the text field use a number pad? 
if (textField.keyboardType != UIKeyboardTypeNumberPad) 
    return; 

// If there's no keyboard yet, don't do anything 
if ([[[UIApplication sharedApplication] windows] count] < 2) 
    return; 
UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 

// Create new custom button 
self.numberPadDoneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
self.numberPadDoneButton.frame = CGRectMake(0, 163, 106, 53); 
self.numberPadDoneButton.adjustsImageWhenHighlighted = FALSE; 
[self.numberPadDoneButton setTitle:@"Return" forState:UIControlStateNormal]; 
// [self.numberPadDoneButton setFont:[UIFont boldSystemFontOfSize:18]]; 
[self.numberPadDoneButton setTitleColor:[UIColor colorWithRed:77.0f/255.0f green:84.0f/255.0f blue:98.0f/255.0f alpha:1.0] forState:UIControlStateNormal]; 

[self.numberPadDoneButton setImage:self.numberPadDoneImageNormal forState:UIControlStateNormal]; 
[self.numberPadDoneButton setImage:self.numberPadDoneImageHighlighted forState:UIControlStateHighlighted]; 
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside]; 

// Locate keyboard view and add button 
NSString *keyboardPrefix = [[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 ? @"<UIPeripheralHost" : @"<UIKeyboard"; 
for (UIView *subView in keyboardWindow.subviews) { 
    if ([[subView description] hasPrefix:keyboardPrefix]) { 
     [subView addSubview:self.numberPadDoneButton]; 
     [self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside]; 
     break; 
    } 
} 
} 

- (void)textFieldDidBeginEditing:(NSNotification *)note { 
[self updateKeyboardButtonFor:[note object]]; 
} 

- (void)keyboardWillShow:(NSNotification *)note { 
[self updateKeyboardButtonFor:[self findFirstResponderTextField]]; 
} 

- (void)keyboardDidShow:(NSNotification *)note { 
[self updateKeyboardButtonFor:[self findFirstResponderTextField]]; 
} 

- (IBAction)numberPadDoneButton:(id)sender { 
UITextField *textField = [self findFirstResponderTextField]; 
[textField resignFirstResponder]; 
} 


- (void)dealloc { 
[numberPadDoneImageNormal release]; 
[numberPadDoneImageHighlighted release]; 
[numberPadDoneButton release]; 
[super dealloc]; 
} 
相關問題