views:

240

answers:

1

Hi, I'm using the trick to put a custom button for the numeric keyboard.

But I can't remove the button after use it, so in regular keyboard appear on top of the view.

This is how I add it:

- (void)keyboardShow:(NSValue *)v 
{
    if (isKeyboardNumeric) {
     // create custom button
     UIButton *doneButton = [[UIButton alloc] init];
     //doneButton.buttonType = UIButtonTypeCustom;
     doneButton.frame = CGRectMake(0, 163, 106, 53);
     doneButton.adjustsImageWhenHighlighted = NO;
     if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
      [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(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside];
     doneButton.tag = 99;
     // 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 view found; add the custom button to it
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
       [keyboard addSubview:doneButton];
     }
    }
}

And this how I remove it:

- (void)keyboardHide
{
    UIView *btn;
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
     keyboard = [tempWindow.subviews objectAtIndex:i];
     // keyboard view found; add the custom button to it
     if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
      for (btn in keyboard.subviews) {
       if (btn.tag==99) {
        [btn removeFromSuperview];
        [btn release];
        break;
       }
      }
     }
    } 
    [self setupButtons];
}

Both methods get called correctly, and in the debugger I confirm that is called

[btn removeFromSuperview]

But anyway, the button remain.

I try to put the code to remove it just before the if (isKeyboardNumeric) call but nothing.

+4  A: 

This is exactly the sort of thing Apple tells you not to do it, and clear you know it because you are attempting to guard yourself by checking OS revisions, etc. This is super fragile, it is entirely possible that this will change inside of minor updates or bugfixes. In 2.x Apple added and modified several keyboards.

The fact you can't get sensible behavior is not surprising, who knows what Apple is doing inside the the keyboard view, they could be retagging or doing custom drawing caching off screen images of the view to increase drawing efficiency.

If you want to do this you should implement your own custom numeric keyboard and pop it in and out when you need it. Probably not the answer you want to hear, but it will probably end up being a lot simpler than hacking around in your views, and a lot less likely to cause problems for your users.

Louis Gerbarg
Ok, that is fair.I change the way the user interface work and remove the hack to use a toolbar on-top the keyboard )
mamcx