0

i have read a lto of questions about this, but just can't find the solution.

I simply wont to add a Done button to my NumberPad

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)addButtonToKeyboard {
    NSLog(@"call");
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;

    [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;

    NSLog(@"%d", [tempWindow.subviews count]);

    for(int i=0; i<[tempWindow.subviews count]; i++) {
        NSLog(@"found");

        keyboard = [tempWindow.subviews objectAtIndex:i];
        [keyboard addSubview:doneButton];
    }
}

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

This code:

NSLog(@"%d", [tempWindow.subviews count]);

is always 0, so it simply does not adds a button... any idea?

Alexander_F
  • 2,831
  • 3
  • 28
  • 61

3 Answers3

0

There is a great Tutorial that does exactly what you need So visit: http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key

Try adding

 if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
        [keyboard addSubview:doneButton];

in your

for(int i=0; i<[tempWindow.subviews count]; i++) {
    NSLog(@"found");

    keyboard = [tempWindow.subviews objectAtIndex:i];
    [keyboard addSubview:doneButton];
}

so this should be your result:

for(int i=0; i<[tempWindow.subviews count]; i++) {
    NSLog(@"found");

    keyboard = [tempWindow.subviews objectAtIndex:i];
    if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
        [keyboard addSubview:doneButton];   
}

Or you could just use an Extension you will find in this post https://stackoverflow.com/a/4355795/1578508

Community
  • 1
  • 1
lukaswelte
  • 2,951
  • 1
  • 23
  • 45
  • But he is not checking of it is the keyboard or not.. And that's why I also putter a link to a extension he can just use – lukaswelte Aug 23 '12 at 15:19
  • That's weird.. But you didn't create another window somewhere? Did you try using the extension you can find by following the link? – lukaswelte Aug 23 '12 at 19:00
0

this my working code for this, it has been used on real iPhone only, I've never tested on Simulator, I'm hoping it will help on you.

- (void)keyboardWillShow:(NSNotification *)notification {
    UIWindow *_keyboardWindow = nil;
    for (UIWindow *_testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[_testWindow class] isEqual:[UIWindow class]]) {
            _keyboardWindow = _testWindow;
            break;
        }
    }

    UIView *_foundKeyboard = nil;
    for (UIView *_possibleKeyboard in [_keyboardWindow subviews]) {
        if ([[_possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
            if ([[[[_possibleKeyboard subviews] objectAtIndex:0] description] hasPrefix:@"<UIKeyboard"]) {
                _foundKeyboard = _possibleKeyboard;
                break;
            }
        } else if ([[_possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            _foundKeyboard = _possibleKeyboard;
            break;
        }
    }

    if (_foundKeyboard) {
        if (_doneButton == nil) {
            _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _doneButton.frame = CGRectMake(0, 163, 106, 53);
            _doneButton.adjustsImageWhenHighlighted = false;
            [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:19.f]];
            [_doneButton setTitle:@"DONE" forState:UIControlStateNormal];
            [_doneButton setTitleColor:[UIColor colorWithRed:77.f/256.f green:84.f/256.f blue:98.f/256.f alpha:1.f] forState:UIControlStateNormal];
            [_doneButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_doneButton.titleLabel setShadowOffset:CGSizeMake(1.f, 1.f)];
            [_doneButton setTitle:@"DONE" forState:UIControlStateHighlighted];      
            [_doneButton addTarget:self action:@selector(buttonKeyBoardDoneTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (_doneButton.superview == nil) {
            [_foundKeyboard addSubview:_doneButton];
        }
    }
}
holex
  • 23,961
  • 7
  • 62
  • 76
0

The real solution is to create a view with all the buttons (12). And then just animate it into view from off screen. With previous poster's method, any updates to iOS's can break the functionality, and you DON'T want that if you have any kind of serious apps. You customers can go crazy and your app is installed over by an alternative.

Down side here is that you need to draw some decent graphics, but guarantee to work till Apple have an official way to let this happen.

mskw
  • 10,063
  • 9
  • 42
  • 64