Search code examples
iphoneuitextfielduikeyboarduikeyboardtype

How set a keyboard for number text field and alphabetic text field?


I have a layout where i have two text fields one is for alphabetic input and another one for numeric input. I have set different type of keyboards for these like as default for alphabetic text field and number for numeric field.

I have added a done button on number pad. Still I am getting this a issue with keyboards that is when I taped on numeric text filed first then it show done button keyboard while I taped alphabetic text filed first and then taped on numeric text field then it doesn't add done button on number pad.

How do I solve it?

I have use these code to add button on number pad.

    -(void)viewWillAppear:(BOOL)animated
       {
        // Register for the events

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

        keyboardVisible = NO;
        scroll_view.contentSize=CGSizeMake(320, 400);
       }



        - (void)keyboardDidShow:(NSNotification *)note {
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
        UIView* keyboard;
        for (keyboard in tempWindow.subviews) {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                if (numberPadShowing) {
                    [self addButtonToKeyboard];
                    return;                 
                    break;
                } else {
                    for (UIView *v in [keyboard subviews]){
                        if ([v tag]==123)
                            [v removeFromSuperview];
                    }
                }
        }
    }

    -

    (void) keyboardDidHide: (NSNotification *)notif 
   {
        // Is the keyboard already shown
        if (!keyboardVisible) {
            return;
        }
        // Keyboard is no longer visible
        keyboardVisible = NO;
    }

     -(void)doneButton 
   {
    UIScrollView *scrollView =[[UIScrollView alloc] init];
    scrollView=scroll_view;
    [scrollView setContentOffset:svos animated:YES]; 
    [myActiveTextField resignFirstResponder];
   }

        - (void)addButtonToKeyboard {
        // create custom button
        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(0, 163, 106, 53);
        doneButton.adjustsImageWhenHighlighted = NO;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
            [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(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];
            }
        }
    }

       - (void)textFieldDidBeginEditing:(UITextField *)textField
       {
        myActiveTextField=textField;
        if (myActiveTextField==txt_hour_section || myActiveTextField==txt_minute_section || myActiveTextField==txt_transport_time){
            numberPadShowing=TRUE;
            UIScrollView *scrollView =[[UIScrollView alloc] init];
            scrollView=scroll_view;

            svos = scrollView.contentOffset;
            CGPoint pt;
            CGRect rc = [textField bounds];
            rc = [textField convertRect:rc toView:scrollView];
            pt = rc.origin;
            pt.x = 0;
            pt.y -= 60;
            [scrollView setContentOffset:pt animated:YES];           

        }
        else{
            numberPadShowing=FALSE;
        }
    }



     -(BOOL)textFieldShouldReturn:(UITextField *)textField
     {
        UIScrollView *scrollView =[[UIScrollView alloc] init];
        scrollView=scroll_view;
        [scrollView setContentOffset:svos animated:YES]; 
        [textField resignFirstResponder];
       return YES;
    }

Thanks in advances...


Solution

  • I think your problem is becuase you are tapping in to the numeric field after tapping the alpha field, and the alpha keyboard is already in view. Therefore when you tap the number field (whilst alpha keyboard still in view) it doesn't fire the 'keyboardDidShow' event, as the keyboard is already showing from the previous field (albeit a alpha one).

    Try putting your done button logic into a delegate method for the textfield 'textFieldDidBeginEditing' event for the number field.