I am trying to recreate something similar to the popup keyboard used in safari.
I am able to visually reproduce it by placeing a toolbar over my view and the appropriate buttons, however i cant figure out any way to dismiss the keyboard once the user has touched the done button.
There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.
If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.
If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:
You must also implement the UITextFieldDelegate for your ViewController.
Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]
All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call the resignFirstResponder your set.
If you're talking about dismissing the keyboard from a
UITextField
rather than aUITextView
. Your question isn't that clear? If you are then ensure your class is marked as aUITextFieldDelegate
in the interface file,and then you should implement the two delegate methods as below,
However if you're using a
UITextView
then things are a bit more complicated. TheUITextViewDelegate
protocol lacks the equivalent to thetextFieldShouldReturn:
method, presumably since we shouldn’t expect the Return key to be a signal that the user wishes to stop editing the text in a multi-line text entry dialog (after all, the user may want to insert line breaks by pressing Return).However, there are several ways around the inability of the
UITextView
to resign as first responder using the keyboard. The usual method is to place a Done button in the navigation bar when theUITextView
presents the pop-up keyboard. When tapped, this button asks the text view to resign as first responder, which will then dismiss the keyboard.However, depending on how you’ve planned out your interface, you might want the
UITextView
to resign when the user taps outside theUITextView
itself. To do this, you’d subclassUIView
to accept touches, and then instruct the text view to resign when the user taps outside the view itself.Create a new class,
Then, in the implementation, implement the
touchesEnded:withEvent:
method and ask theUITextView
to resign as first responder.Once you’ve added the class, you need to save all your changes, then go into Interface Builder and click on your view. Open the Identity inspector in the Utility pabel and change the type of the view in your nib file to be your
CustomView
rather than the defaultUIView
class. Then in the Connections Inspector, drag thetextView
outlet to theUITextView
. After doing so, and once you rebuild your application, touches outside the active UI elements will now dismiss the keyboard. Note however that if theUIView
you are subclassing is “behind” other UI elements, these elements will intercept the touches before they reach the UIView layer. So while this solution is elegant, it can be used in only some situations. In many cases, you’ll have to resort to the brute force method of adding a Done button to the navigation bar to dismiss the keyboard.If your building your own views in Interface Builder, set your view controller to be delegate for the text field and implement textFieldShouldReturn: from UITextFieldDelegate in your views controller.
UITextFieldDelegate textFieldShouldReturn: in the iphone cocoa docs
Have you tried:
where
viewReceivingKeys
is the UIView that is receiving the text input?use a navigation controller and pop the view when done?
for example, I use code like this to slide an about box in:
and then when the button in that about box is clicked, I use this to get rid of it:
In my case the about box occupies the whole screen, but I don't think it would have to for this to work.
edit: I think I may have misunderstood your question. Something along the lines of my code would be if you are faking the whole keyboard view yourself. I think that resign first responder is the right way to do it if it is the normal keyboard with your toolbar added on top.