How to dismiss number pad keyboard by tapping anywhere

后端 未结 15 2206
梦如初夏
梦如初夏 2020-12-04 17:53

I\'d like to know the simplest code to dismiss the number pad keyboard when tapping anywhere outside the number pad. It\'s a simple application to input a number inside a te

15条回答
  •  广开言路
    2020-12-04 18:00

    Touching the Background to Close the Keyboard

    Go to Xcode if you’re not already there. We need to add one more action to our controller class. Add the following line to your Control_FunViewController.h file:

    #import 
    @interface Control_FunViewController : UIViewController { 
    UITextField *nameField;
    UITextField *numberField;
    }
     @property (nonatomic, retain) IBOutlet UITextField *nameField; ]
    @property (nonatomic, retain) IBOutlet UITextField *numberField; 
    - (IBAction)textFieldDoneEditing:(id)sender; 
    - (IBAction)backgroundTap:(id)sender; 
    @end  
    

    Save the header file; switch over to the implementation file, and add this code, which simply tells both text fields to yield first responder status if they have it. It is perfectly safe to call resignFirstResponder on a control that is not the first responder, so we can safely call it on both text fields without having to check whether either is the first responder.

    - (IBAction)backgroundTap:(id)sender { 
    [nameField resignFirstResponder]; 
    [numberField resignFirstResponder];
    }
    

    TIP Save this file, and go back to Interface Builder. We now need to change the underlying class of our nib’s view. If you look at the nib’s main window , you’ll see that there are three icons in that view. The third one, called View, is our nib’s main view that holds all the other controls and views as subviews. Single-click the icon called View, which represents our nib’s container view. Press ␣4 to bring up the identity inspector. This is where we can change the underlying class of any object instance in Interface Builder.

    You’ll be switching between header and implementation files a lot as you code. Fortunately, Xcode has a key combination that will switch you between these files quickly. The default key combination is ␣␣␣ (option-command-up arrow), although you can change it to anything you want using Xcode’s preferences.76

    The field labeled Class currently says UIView. Change it to read UIControl. All controls that are capable of trig- gering action methods are subclasses of UIControl, so by changing the underlying class, we have just given this view the ability to trigger action methods. You can verify this by pressing ␣2 to bring up the connections inspector.

    Drag from the Touch Down event to the File’s Owner icon, and choose the backgroundTap: action. Now, touches anywhere in the view without an active control will trigger our new action method, which will cause the keyboard to retract.

    NOTE Save the nib, and let’s go back and try it. Compile and run your application again. This time, the keyboard should disappear not only when the Done button is tapped but also when you click anywhere that’s not an active control, which is the behavior that your user will expect.

提交回复
热议问题