How -To Programmatically Dismiss The UI Keyboard On IPhone Or IPad Apps

how to iphone apps on ipad
Computers you can also make the keyboard dismiss when the user moves the focus to another UITextField or presses a button on the UI. You can also make the keyboard close using finger gestures like a swipe. In this tutorial, I will demonstrate these three use cases and this should give you all the building blocks to devise other methods of dismissing the keyboard in your own apps. This first example to demonstrate how to make the keyboard move to the background when a UITextField loses focus. Start by create a new project like a Single-View Application. Make sure the Storyboard and ARC (Automatic Reference Counting) is selected in the Options page. Next open the storyboard and add two TextFields and labels (optional). See Figure 1 below. Select the first TextField and open the Attributes inspector. In the TextField properties, you will notice a Keyboard field. Leave the value at Default. You can choose other options like a numerical pad depending on your needs but this example the Default value is fine.

how to iphone apps on ipad
In the Return Key field, select the “Done” value and also set the auto-enable the Return Key property. Repeat the sample process for the second TextField. To make the keyboard dismiss when TextField looses focus you will need to add a Delegate from the TextField to the View Controller Object . That is the yellow globe in the black bar at the bottom of the View Controller. When you release the mouse button, a popover will appear. Repeat the same process for the second UITextField. Once that is complete, open the Assistant Editor and add two IBOutlets to the ViewController header file one for each UITextField and an IBAction for the button. If you help this operation, read this tutorial on creating Connection and Delegates. The textFieldShouldReturn method is defined in the UITextField class. It responds to the delegate that you created earlier and checks to see which UITextField is calling the method and dismisses the keyboard accordingly.

This is nice and simple. When the use click on the “Done” button on the keyboard, the keyboard moves to the background. This second example builds on the first and basically uses the same code as in the first example. In the above code, the delegate checks to see which UITextField is active (has the focus) and dismisses the keyboard by call the resignFirstResponder for the appropriate delegate call. If you run the code and enter some text in one of the field and click on the “Dismiss Keyboard” button, the keyboard will move to the background as before. This third and final example will dismiss the keyboard based on a gesture from the user. To make this work add a Swipe Gesture Recognizer (Figure 7). You will find these under Gesture Recognizers in the Object Library. To respond to swipes in both directions, you would need two Swipe Gesture Recognizers but for out example we will only use one to detect a downward swipe.

First drag and drop a Swipe Gesture Recognizer on to the View Controller. One more step in our Swipe configuration. Select the Swipe in the Dock and switch to the Attributes assistant and select the type of swipe to perform. You can select “Up”, “Down”, “Left” or “Right”. For this example, select “Down”. Now add a Delegate for our Swipe. Open the storyboard again and drag a connection to the View Controller Object in the Dock. Choose the method you just create in the Outlet popover. All is needed now to add some code to your new method. If you run your app again, use the mouse pointer and drag the pointer downwards and release the mouse button. The keyboard will dismiss. This covers most of the use cases for dismissing the keyboard. You can implement and adapt this code in this tutorial to fit your needs. Also build on these examples to build use the keyboard in other ways.

Post a Comment

0 Comments