2010-06-10 44 views
24

我想給iPhone數字鍵盤添加一個完成按鈕。就在這樣一個按鈕的左下方還有一個方便的空間。如何在iPhone OS 4的數字鍵盤上顯示按鈕'完成'?

以前,我是用相似的技巧那些Question 584538Luzian Scherrer's excellent blog post描述,而是停在的iOS 4的工作,我可以通過創建自定義inputView做到這一點,但我更願意去繼承寫作的蘋果鍵盤我自己的。

有沒有新的方式添加視圖到標準鍵盤?有人爲此發佈了OSS inputView嗎?有另一種方法嗎?

回答

3

Luzian Scherrer's blog post中描述的技術可以在iOS 4.0中使用。

我的代碼將按鈕添加到鍵盤失敗,因爲它是從textFieldDidBeginEditing:委託方法中調用的,該方法在創建鍵盤窗口的子視圖之前調用該方法(在4.0中)。當我觀察到UIKeyboardWillShowNotification時,我通過調用我的代碼解決了問題,這種情況發生得很晚。

+0

在4.2,這並不工作。 – WrightsCS 2010-11-09 03:19:48

+1

我與4.2有同樣的問題。爲什麼這麼複雜?!?!蘋果?!? – AYBABTU 2010-12-09 17:42:31

+1

在我的應用程序中,我有兩個文本字段。一個將顯示數字鍵盤和其他文本字段將顯示正常的字母鍵盤。我只想爲數字鍵盤輸入完成按鈕。我怎樣才能防止它的普通鍵盤? – Satyam 2012-06-27 18:42:27

6

我得到了這個工作。看到這裏的代碼:http://gist.github.com/454844

有兩個問題:

  1. UIKeyboardWillShowNotification鍵盤視圖是否存在之前被髮送,但您安排您的代碼在下次運行環通運行,他們確實存在。所以你不必打擾DidShow這是不太令人滿意的。

  2. 在iOS 4上,UIKeyboard視圖位於視圖層次結構中的其他位置。

+0

你救了我! ... – mshsayem 2010-07-01 10:31:44

+0

我比你使用inputAccesoryView更喜歡你的解決方案。真棒!感謝分享! – Mathieu 2012-11-07 10:46:34

2

我已經提交給蘋果作爲一個錯誤。 Interface Builder允許開發人員爲鍵盤類型的數字鍵盤指定返回鍵類型。錯誤號8759674.

蘋果跟進說,這個問題以前已經記錄爲錯誤ID#5885964和他們關閉8759674.

1

你可以使用這個鍵盤製作上的數字完成按鈕通知碼keypad.You必須在您的最後下載done.png圖片。

在.h文件中

{  //Keyboard Hide 
UIImage *numberPadDoneImageNormal; 
UIImage *numberPadDoneImageHighlighted; 
UIButton *numberPadDoneButton; 
} 

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

- (IBAction)numberPadDoneButton:(id)sender; 

在.m文件

@synthesize numberPadDoneImageNormal; 
@synthesize numberPadDoneImageHighlighted; 
@synthesize numberPadDoneButton; 

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { 
if ([super initWithNibName:nibName bundle:nibBundle] == nil) 
    return nil; 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) { 
    self.numberPadDoneImageNormal = [UIImage imageNamed:@"NumberDone.png"]; 
    self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"NumberDone.png"]; 
} else {   
    self.numberPadDoneImageNormal = [UIImage imageNamed:@"NumberDone.png"]; 
    self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"NumberDone.png"]; 
}   
return self; 
} 

- (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]; 
} 
1

我創建了一個版本,以使數字鍵盤的橫版... 插頭插入Luzian謝樂的博客中描述的技術,並新增兩個額外的PNG圖像(這裏不包括),以及「isLandscape」布爾成「didRotateFromInterfaceOrientation」方法..

@interface AlarmController() 
    { 
     CGRect doneButtnRectVert; 
     CGRect doneButtnRectHorz; 
     CGRect doneButtnRect; 
     UIImage* DoneUpVert; 
     UIImage* DoneDownVert; 
     UIImage* DoneUpHorz; 
     UIImage* DoneDownHorz; 
     UIImage* DoneUp; 
     UIImage* DoneDown; 
     UIButton *oldDoneButton; 
    } 
    @end 

    //--------------------------------------------------------------------------- 
    - (void)viewDidUnload 
    { 
    NSLog(@"viewDidUnload AlarmController"); 
     [[NSNotificationCenter defaultCenter] removeObserver:self 
             name:UIKeyboardDidShowNotification object:nil]; 

     [super viewDidUnload]; 
    } 

    //--------------------------------------------------------------------------- 
    - (void)addButtonToKeyboard 
    { 
    NSLog(@"addButtonToKeyboard AlarmController"); 
     if (isLandscape) 
     { 
      doneButtnRect = doneButtnRectHorz; 
      DoneUp = DoneUpHorz; 
      DoneDown = DoneDownHorz; 
     } else { 
      doneButtnRect = doneButtnRectVert; 
      DoneUp = DoneUpVert; 
      DoneDown = DoneDownVert; 
     } 

     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     doneButton.frame = doneButtnRect; 
     doneButton.adjustsImageWhenHighlighted = NO; 
     [doneButton setImage:DoneUp forState:UIControlStateNormal]; 
     [doneButton setImage:DoneDown forState:UIControlStateHighlighted]; 

     [doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside]; 

     UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
     UIView* keyboard; 
     for(int i=0; i<[tempWindow.subviews count]; i++) 
     { 
      keyboard = [tempWindow.subviews objectAtIndex:i]; 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
      { 
       if (oldDoneButton) [oldDoneButton removeFromSuperview]; 
       [keyboard addSubview:doneButton]; 
      } 
     } 
     oldDoneButton = doneButton; 
    } 

    //--------------------------------------------------------------------------- 
    - (void)keyboardDidShow:(NSNotification *)note 
    { 
    NSLog(@"keyboardDidShow AlarmController"); 
     [self addButtonToKeyboard]; 
    } 

    #pragma mark - 
    #pragma mark meaty area... 

//--------------------------------------------------------------------------- 
- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
NSLog(@"textFieldDidEndEditing AlarmController"); 
    oldDoneButton = nil; 
} 

    //--------------------------------------------------------------------------- 
    - (void)viewDidLoad 
    { 
    NSLog(@"viewDidLoad AlarmController"); 
     [super viewDidLoad]; 
     doneButtnRectVert = CGRectMake(0, 163, 106, 53); 
     doneButtnRectHorz = CGRectMake(0, 122, 159, 40); 
     DoneUpVert = [UIImage imageNamed:@"DoneUp3.png"]; 
     DoneDownVert = [UIImage imageNamed:@"DoneDown3.png"]; 
     DoneUpHorz = [UIImage imageNamed:@"DoneUpHor.png"]; 
     DoneDownHorz = [UIImage imageNamed:@"DoneDnHor.png"]; 

     doneButtnRect = doneButtnRectVert; 
     DoneUp = DoneUpVert; 
     DoneDown = DoneDownVert; 
     oldDoneButton = nil; 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(keyboardDidShow:) 
                name:UIKeyboardDidShowNotification object:nil];   
    } 
89

你可以用「應用」添加inputAccessoryView和「取消」按鈕,並與當時解僱數字鍵盤。

inputAccessoryView

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 

    numberToolbar.items = [NSArray arrayWithObjects: 
     [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], 
     [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
     [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], 
     nil]; 

    numberTextField.inputAccessoryView = numberToolbar; 
} 

-(void)cancelNumberPad{ 
    [numberTextField resignFirstResponder]; 
    numberTextField.text = @""; 
} 

-(void)doneWithNumberPad{ 
    NSString *numberFromTheKeyboard = numberTextField.text; 
    [numberTextField resignFirstResponder]; 
} 
+0

謝謝,它真的幫助 – 2012-07-12 14:01:42

+1

歡迎您:) – Luda 2012-07-23 07:49:05

+0

令人驚歎!謝謝 – 2012-09-10 09:44:37

相關問題