done button on numberkeypad is not working

609 views Asked by At

I am using a text field to enter mobile nuber and I would like to add done button to hide the keyboard, following is my code

    - (void)keyboardWillShow:(NSNotification *)note {  
        // 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;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
   }
}

but I am unable to add the done button ,after cross check with break points , I observe that control is not entering into the if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES) conditon. I am using IOS5.

1

There are 1 answers

5
TRD On BEST ANSWER

I did add a custom done-button in one of my projects too. The tutorial I used mentioned that piece of code:

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
    if([[tmpKeyboard description] hasPrefix:@"<UIPeripheralHost"] == TRUE)
        [tmpKeyboard addSubview:doneButton];
}
else
{
    if([[tmpKeyboard description] hasPrefix:@"<UIKeyboard"] == TRUE)
        [tmpKeyboard addSubview:doneButton];
}

Prior to iOS version 3.2 your approach with UIKeyboard is fine, but later you have to change it to UIPeripheralHost.