2010-06-10 15 views
24

Vorrei aggiungere un pulsante Fine alla tastiera del tastierino numerico dell'iPhone. C'è anche un comodo spazio in basso a sinistra per un pulsante di questo tipo. In precedenza, stavo usando un trucco simile a quelli descritti in Question 584538 e Luzian Scherrer's excellent blog post, ma questo ha smesso di funzionare in iOS 4. Posso farlo creando un inputView personalizzato, ma preferirei estendere la tastiera di Apple invece di scrivere il mio.Come mostrare il pulsante 'Fatto' sul tastierino numerico su iPhone OS 4?

C'è un nuovo modo per aggiungere una vista alla tastiera standard? Qualcuno ha pubblicato un inputView OSS per questo? C'è un altro modo?

risposta

3

La tecnica descritta in Luzian Scherrer's blog postfa funziona in iOS 4.0.

Il mio codice per aggiungere il pulsante alla tastiera non funzionava perché veniva chiamato dal metodo delegato textFieldDidBeginEditing:, che (in 4.0) viene chiamato prima che vengano create le sottoview della finestra della tastiera. Ho risolto il problema chiamando il mio codice quando si osserva UIKeyboardWillShowNotification, il che accade abbastanza tardi.

+0

questo non funziona su 4.2. – WrightsCS

+1

Ho lo stesso problema con 4.2. Perché è così complicato?!?! MELA?!? – AYBABTU

+1

Nella mia app ho due campi di testo. Uno mostrerà il tastierino numerico e l'altro campo di testo mostrerà la normale tastiera alfabeti. Voglio avere il pulsante Done solo per il tastierino numerico. come posso prevenirlo per tastiera normale? – Satyam

6

Ho ottenuto questo lavoro. Vedere il codice qui: http://gist.github.com/454844

C'erano due problemi:

  1. UIKeyboardWillShowNotification viene inviato prima che esista la vista della tastiera, ma si pianifica il codice per eseguire il prossimo passaggio del ciclo di esecuzione, esistono. Quindi non devi preoccuparti di DidShow che è visivamente meno piacevole.

  2. Su iOS 4 la vista UIKeyboard era altrove nella gerarchia della vista.

+0

mi hai salvato! ... – mshsayem

+0

Preferisco la soluzione piuttosto che usare inputAccesoryView. Eccezionale! Grazie per la condivisione! – Mathieu

2

Ho inviato questo ad Apple come un bug. Interface Builder consente allo sviluppatore di specificare un tipo di tasto di ritorno per un tipo di tastiera del tastierino numerico. numero di bug 8759674.

Mela seguito da sé che questo problema è stato registrato in precedenza come ID bug # 5.885.964 e hanno chiuso 8759674.

1

È possibile utilizzare questa tastiera notificare codice per fare il tasto fatto sul numero keypad.You devi solo scaricare immagine done.png alla tua fine.

In .h file di

{  //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; 

Nel file di .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

Ho creato una versione per attivare la versione orizzontale del tastierino numerico ... spina nella tecnica descritta nel blog di Luzian Scherrer e aggiungere i due PNG supplementari (non incluso qui), e il booleano "isLandscape" in modalità "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

È possibile aggiungere inputAccessoryView con 'Applica' e 'Annulla' pulsanti, e respingere il tastierino numerico con l'allora.

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

Grazie, mi ha davvero aiutato –

+1

Siete i benvenuti :) – Luda

+0

Incredibile! grazie –

Problemi correlati