2

I am new in IOS & I am using IOS 6. I have more than one textfield in my code, one of them uses the Number pad keyboard and I want to add the custom button to it.

I am using this code:

 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;
    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:@"<UIPeripheralHost"] == TRUE)
            [keyboard addSubview:doneButton];
      }

Selector is called, but the problem is that the [[tempwindow.subviews]count] is 0.

Could anyone help me ?

Thanks in advance.

Tallmaris
  • 7,605
  • 3
  • 28
  • 58

3 Answers3

2

You can do this by checking following links.

Reference 1

Reference 2

But please do not do this.

Consider this:

  • what if the layout of the number keypad changes?
  • what if the colors of the keys change?
  • what if the implementation of the keyboard changes such that it's no longer the window at index 1?
  • what if the implementation of the keyboard and peripheral host change such that introspecting the description breaks?
  • what if you're running this on iPad where the keyboard has a totally different layout?
Shardul
  • 4,266
  • 3
  • 32
  • 50
  • The problem is that the count of the for loop is 0, i means `UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];` the `[[tempWindow.subViews] count]` is zero – محمود مندور May 20 '13 at 09:34
1

For me, I am using the following code snippet to add button to the keyboard:

- (void)addHideKeyboardButtonToKeyboard {
    // Locate non-UIWindow.
    UIWindow *keyboardWindow = nil;
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[testWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = testWindow;
            break;
        }
    }
    if (!keyboardWindow) return;

    // Locate UIKeyboard.
    UIView *foundKeyboard = nil;
    for (__strong UIView *possibleKeyboard in [keyboardWindow subviews]) {
        // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
        if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
            for (__strong UIView *anotherPossibleKeyboard in [possibleKeyboard subviews]) {       
                if ([[anotherPossibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
                    foundKeyboard = possibleKeyboard;
                    break;
                }
            }
        }
    }

    if (foundKeyboard) {
        [foundKeyboard addSubview:self.keyboardDoneButton];        
    }   
}
Valent Richie
  • 5,226
  • 1
  • 20
  • 21
0

I have used following code. Check it.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(textField==txt1)
    {

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

    }
    else
    {

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    }



}
- (void) keyboardDidShowOrHide : (id) sender {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 427, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setBackgroundImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
    [doneButton setBackgroundImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    [tempWindow addSubview:doneButton];


}
-(void)doneButton:(id)sender
{
    UIButton *btntmp=sender;
    [btntmp removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    //[self setViewMovedUp:NO];
    [txt1 resignFirstResponder];
}
Shardul
  • 4,266
  • 3
  • 32
  • 50