Jak wyświetlić przycisk "Gotowe" na klawiaturze numerycznej w iPhone OS 4? [duplikat]

To pytanie ma już odpowiedź tutaj:

Chciałbym dodać przycisk Gotowe do klawiatury iPhone numer pad. Jest nawet poręczne miejsce w lewym dolnym rogu na taki przycisk.

Poprzednio używałem podobnej sztuczki do tych opisanych w pytanie 584538 i [[15]}Luzian Scherrer ' s excellent blog post, ale to przestało działać w iOS 4. Mogę to zrobić, tworząc Niestandardowy inputView, ale wolałbym rozszerzyć klawiaturę Apple zamiast pisać własną.

Czy istnieje nowy sposób dodawania widoku do standardowej klawiatury? Czy ktoś opublikował do tego INPUTVIEW OSS? Jest inny sposób?

Author: Community, 2010-06-10

7 answers

Możesz dodać inputAccessoryView za pomocą przycisków "Zastosuj" i "Anuluj", a następnie odrzucić klawisz numeryczny.

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];
}
 89
Author: Luda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-20 17:55:29

Działa. Zobacz kod tutaj: http://gist.github.com/454844

Były dwie kwestie:

  1. UIKeyboardWillShowNotification jest wysyłany przed pojawieniem się widoku klawiatury, ale jeśli zaplanujesz swój kod do uruchomienia na następnym przejściu pętli run, one istnieją. Więc nie musisz się przejmować DidShow, co jest wizualnie mniej przyjemne.

  2. W systemie iOS 4 widok UIKeyboard znajdował się w innym miejscu hierarchii widoków.

 6
Author: Henrik N,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-27 11:53:46

Technika opisana w Luzian Scherrer ' s blog post czy działa w iOS 4.0.

Mój kod dodawania przycisku do klawiatury nie powiódł się, ponieważ był wywoływany z metody delegate textFieldDidBeginEditing:, która (w 4.0) jest wywoływana przed utworzeniem podwidi okna klawiatury. Naprawiłem ten problem, wywołując mój kod, gdy UIKeyboardWillShowNotification jest obserwowany, co dzieje się wystarczająco późno.

 3
Author: Will Harris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-02 15:38:54

Zrefakturowałem kod i umieściłem go na GitHubie.

Https://github.com/dazuiba/dz-numberpad-done-helper

 3
Author: dazuiba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-10 09:26:46

Zgłosiłem to do Apple jako błąd. Interface Builder pozwala programiście określić typ klawisza zwrotnego dla typu klawiatury klawiatury numerycznej. Błąd numer 8759674.

Następnie Apple stwierdziło, że ten problem został wcześniej zarejestrowany jako Bug ID # 5885964 i zamknęli 8759674.

 2
Author: Alan Hancock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-21 17:49:47

Możesz użyć tego kodu powiadomień klawiatury do wykonania przycisku na klawiaturze numerycznej.Musisz po prostu pobrać gotowe.obraz png na końcu.

W .plik 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;

W .plik 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
Author: sinh99,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-27 06:23:40

Stworzyłem wersję, aby włączyć poziomą wersję numpada... podłącz technikę opisaną na blogu Luziana Scherrera i dodaj dwa dodatkowe pngs( nie zawarte tutaj), a" isLandscape "boolean do metody "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];        
    }
 1
Author: hokkuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-30 16:03:55