UITextField Hide Keyboard But Reamin First Responder? - iphone

I've subclassed a UITextField to display a UIDatePicker instead of a keyboard. Entering dates is something that happens often I our app. The problem occurs when another of our custom classes that accommodates the keyboard needs to know what the first responder.
Is there a way to remain first responder, whilst hiding keyboard?

No, you have to resign as responder (give up focus) to dismiss the keyboard.
Edit: It seems I lied. Try [self.view endEditing:YES];
FYI: It only works on iOS 3.2+

Actually, instead of subclassing, you should just make a custom inputView for your text field which uses a date picker as the custom "keyboard". Then, it will remain the first responder and never even call the system keyboard in the first place.

Related

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.

Remove keyboard or place Picker View on TOP of keyboard

Anyone has any idea how one can achieve the same look as when you press on the "From:" in the Mail iphone app when you have more than one user account.
I'd like to remove my keyboard, but WITHOUT animating it. Rather just disappear and instead of it have a UIPickerView appear.
Thanks much!
Since iOS 3.2 you can set the inputView property of a UITextField to a custom view. This view is then shown instead of the keyboard as input for that view.
There's no way to achieve this before iOS 3.2 I know.
UITextField Class Reference

UICatalog and Keyboard Events

The latest version of Apple's UICatalog example application includes zero code in the TextFieldController for handling keyboard show/hide events, and yet the table view still slides up and down beautifully with the keyboard.
Does anyone know what the new trick is? Are there settings in the XIB that allowed them to forgo registering for the notifications or using TextField delegate methods?
The TextViewController still uses keyboard notifications to deal with view sliding, so I'm really confused as to why this isn't included for TextFields anymore.
Thoughts?
You can close the keyboard, if it's open by calling:
[sender resignFirstResponder];
Not sure about opening the keyboard however.
The trick is hidden within calling becomeFirstResponder on a UITextField that is in a scrollable view. Apparently, whenever calling [textField becomeFirstResponder], iOS automatically scrolls the parent view until said textField is visible.
This behavior can actually be undesirable in some cases, as it will not usually scroll to the same location that the UIScrollView method scrollRectToVisible:animated: would if you were to try to do things that way.
Thanks for your thoughts everyone!

UITextField focus

How can I programmatically assign focus to a specific UITextField in a view? I have several fields and on view display, I'd like to put the cursor on a specific field, not the first one at top.
Try doing this in viewWillAppear:
[desiredField becomeFirstResponder];
By making the field the first responder, it have focus and the keyboard will be displayed.
Set the first responder for your view to be the text field. This can be done in IB.
In swift:
desiredField.becomeFirstResponder()

TextField with Keyboard in cocos2d !

I am trying to develop a iphone app by using cocos2d. I create a alert view with a text field. when I touch the text field then comes the keyboard. But I want that when the alert is open, in the same time textfield will be selected and keyboard comes(without any touch). how can it posible ? sorry for my bad English.
[myTextField becomeFirstResponder] will probably do what you want.
That would be a little tricky to do. The controls in iPhone use the concept of "first responders" Any events will be handled by the first responder in the controller. Now, when an alert view is displayed, it becomes the first responder so it can respond to button clicks. When a text field is selected by the user, the keyboard gets the control. So I guess what you want to achieve can be done by making the text field the first responder after showing the alert ([txtField becomeFirstResponder])
But I have no idea how the alert view's responses will be handled then. You won't be able to click the OK button on the alert view till the keyboard is dismissed and you resign the first responder of the text field. ([txtField resignFirstResponder]) (This is just a guess, you will have to check the final behavior)
[myTextField becomeFirstResponder] works -- I tested it.