How do I dismiss the keyboard in a custom keyboard? - swift

I would like to assign a key/button on my custom keyboard to dismiss/hide the keyboard. I can't seem to get any code to work.
I have tried:
self.view.resignFirstRespoder()
view.endEditing(true)
But nothing along these lines seems to work.
Could someone please point me in the right direction?

Call the dismissKeyboard() function.
Because a custom keyboard does not have access to the current text
input object, you cannot send it a resignFirstResponder() message (as
you would to dismiss the system keyboard when you are developing an
app with text entry). To dismiss the custom keyboard, call
dismissKeyboard() instead.
Apple reference.

Related

Want to hide keyboard with the press of a button in iOS

I've got an app with user input to enter some numbers and instead of having to push the done key on the keyboard to hide it I'd like for the keyboard to go away when they press the calculate button but I have not been able to figure out how to do this. Some hints please?
Thanks
In iOS you need to "resign" from being first responder in order for the keyboard to be removed from the screen. Simply call resignFirstResponder on your textfield:
[_yourTextFieldWithNumbers resignFirstResponder];
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/resignFirstResponder

iPhone- After resigning first responder on UITextField, can't refocus it

I have a modal window that's used for searching data from a remote server- it has a UITextField as the titleControl of the navbar for the window, and a tableview filling the window (that displays the results obviously). Now what I want to do is when the user scrolls the tableview, immediately have the textfield lose focus (resign first responder) so that the keyboard dismisses and the user has more room to scroll through the tableview (it stretches down to fill the gap left by the keyboard). Basically the same functionality as when using a UISearchDisplayController (or whatever it's called).
So I have this code for detecting the scroll event of the tableview:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[searchField resignFirstResponder];
}
Which works fine. However, the issue is that once the user scrolls the table and the textfield loses focus, you can't give focus back to it by tapping on it again. So basically once I call that [resignFirstResponser] I can never again bring the keyboard back up and edit the textfield value. Anyone have any idea why? Do I need to explicitly call [becomeFirstResponder] on the field somewhere? Because I thought that was handled automatically when the field is tapped?
Also of note- I am calling [becomeFirstResponder] on the text field right when the modal window is first called up, so the field is pre-focused. Could that have anything to do with it?
I can post more code if anyone would like, but I don't think I'm doing anything out of the ordinary with the textfield.
Thanks for any help!
You are calling the resignFirstResponder from a function which will be called everytime you scroll the UIScrollview. Hence it does not appear. You need to call resign when the uitextview goes out of focus.
You can do the following. Its a hack:
Whenever you focus on the UITextField create a invisible button to overlay your scroll view.
Capture the button press event and resign first responder
Whenever the uitextfield becomes first responder create the button
This way you will remove the bug, viz calling the method in scrollViewWillBeginDragging.
Other option would be to overrite viewDidAppear method for the uiTextField.
Or you could put your textfield into a different container and handle scrollViewWillBeginDragging by checking which scrollview sent the message.
Did u set a delegate for you searchField? I had the same issue. I popup a model view, and set the text field to be the first responder inside viewDidLoad. Everything works well for the first time. But once I dismiss the modal view controller, and reopen it. my text field cannot be focused anymore.
I found it has something to do with methods of UITextFieldDelegate. Once I remove implementation for methods
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
everything works well. but don't know why
Are you doing anything with "textFieldShouldEndEditing", like #fengd?
A problem that I had was that I was viewing a modal view, and my "textFieldShouldEndEditing" routine was incorrectly returning "NO" on a specific text field. When my modal got dismissed, I would be unable to tap on any other text-field, presumably because the old text field was still "first responder". Since it can never end editing, it fouls up all other text fields that come after it.
I realize this is 2 yrs after the fact, but maybe someone else might find this useful.

Does Control in IPhone has Lost Focus Action?

In Vb.net we have that sort of thing right.
However, in beginning IPhone development, by David Mark, the way the author implement removing keyboard when a user tap outside the text box is by turning the whole view into a control and then create an IBaction for that view being touch.
Why not the text box touch up outside or touch down outside? Is this the normal way?
Yes it is the normal way of doing out of focus. Or you can implement UITextFieldDelegate metod textFieldShouldReturn. Where you call resignFirstResponder for a particular text field.

Textfield in the keyboard

How do you make a keyboard have a textfield inside like the messages app ?
never tried but you can try this -
You can programmatically
set a tool bar.
set a textfield.
set the tool bar as the inputAccessoryView of the textfield.
make the textfield the first responder.
It depends what you're trying to do with it. You could simply create a view that looks like the keyboard section and show it right above the keyboard. If you're trying to actually send SMSs/emails, look up the MessageUI documentation.

How do you launch a UITextView (iPhone) in edit mode?

I'm working on an iPhone app with only one view, a UITextView, and I want it to launch all ready for typing (with the keyboard activated) but by default it requires the user to first tap the view before the keyboard appears. I've seen this done in other applications but haven't been able to figure it out. Thanks.
you need to use the -becomeFirstResponder method on your field:
[myTextField becomeFirstResponder];
This will pop the keyboard up. To hide the keyboard, use -resignFirstResponder.