0

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...

Yuvaraj.M
  • 9,601
  • 16
  • 69
  • 98
ios
  • 552
  • 5
  • 22

2 Answers2

1

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.

ross_t
  • 511
  • 5
  • 10
0

If my first answer doesn't work in your implementation, then you might want to consider a restructure and go for setting the inputAccessoryView of your UITextField. Code along these lines should do the trick (you can put this in your viewDidLoad nib, and also note that you'll need to implement the doneTapped method yourself);

// Create a UIToolbar object and set its style to be black and transparent
    numericInputAccessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    numericInputAccessoryView_.barStyle = UIBarStyleBlackTranslucent;

// The flexible space item will push the done button to the far right
    UIBarButtonItem *space = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

// Make the done button, this is a system item so you don't need to set the title or anything. This will call a method called "doneTapped:(id)sender" when you tap it, you'll need to implement that yourself.
    UIBarButtonItem *done = [[UIBarButtonItem alloc]WithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)];

// Stick the flexible space and the done button in your toolbar    
    numericInputAccessoryView_.items = [NSArray arrayWithObjects:space,done, nil];

// This would return it, if you had this in a separate method. 
    return numericInputAccessoryView_;
ross_t
  • 511
  • 5
  • 10