UITextField and hiding the numeric keypad

    I want to talk about the way to hide the numeric keypad on the iPhone / iPad, which I had to use not so long ago.

    As you know, when a UITextField receives focus, it displays a keyboard: alphanumeric or numeric, depending on the type. You can hide this keyboard either by clicking on the Done button on it (which can have various types and labels, it can be configured) or by calling the method: [textField resignFirstResponder]Unfortunately for developers, there is no Done button on the numeric keypad, it is completely empty in its place, and therefore remove the keyboard is only possible by calling this method. Prior to the release of iOS 3.2 and 4.0, this was solved by adding your button to this place (the method is dirty, but it worked and everyone was happy): neoos .
    But after the release of these systems, the method stopped working. Another hack appeared for this method, but in my humble opinion it is not worth it.

    Therefore, I decided to do a break of a bunch of links this way: let the user click somewhere except the keyboard, and it should disappear, in my opinion quite intuitively. Since there is a bunch of controls in the place where there is no keyboard, then assigning one of them to someone by subscribing to their events is not an option, so I decided to just show the transparent button on top of everything and hide the keyboard by its UIControlEventTouchDown event. It is worthwhile to show the button only when the keyboard appears, and when you click on the button, hide the keyboard and destroy the button. Implemented this as follows:

    Header file (this is so that you can remember it and later destroy it): Also here you need to sign the class to the UITextFieldDelegate protocol so that you can catch the moment the keyboard is displayed. For some reason, the standard subscription to UITextField events does not work. Well, in Interface Builder you need to specify UITextField that its delegate is the controller class, or which one you will be responsible for handling events. Implementation file: That's all. The solution may not be the best, because the controls under the invisible button become unavailable, and you will have to press once to remove the keyboard, and then you can click on the control. But it’s easy to implement, and in my opinion quite user-friendly.
    UIButton* btnInvisible;




    #pragma mark -
    #pragma mark UITextField delegate
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
    //Create button
    btnInvisible = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
    [btnInvisible addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchDown];

    //Show over the window view, which is at index 0 (usually)
    CorreasAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate.window insertSubview:btnInvisible atIndex:1];

    return YES;
    }

    - (void)hideKeyboard
    {
    //Hide keyboard
    [quantityField resignFirstResponder];

    //Hide and release button
    [btnInvisible removeFromSuperview];
    [btnInvisible release];
    }


    Also popular now: