UITextField becomeFirstResponder works only once - iphone

In my app, there is the ability for the user to input their name.
A UITextField is added to the view and becomeFirstResponder is called.
In the textFieldShouldReturn method, resignFirstResponder is called.
Then in textFieldShouldEndEditing, the UITextField is removed from the view.
This all works fine, but the problem is that when the user tries to input their name a second time, the UITextField shows but keyboard does not.
I have tried lots of things, like moving around become/resign firstresponder or retaining/not retaining the textfield, but I just cant seem to get it to work.
Any ideas?
Thanks

If you have a property set for that UITextField, make sure to set it to nil after removing it from the view.

try doing the work in textFieldDidEndEditing, and when you tap on text field second time, control should go in textFieldShouldBeginEditing, try checking that with break points, it it does not goes then problem is that your textfield does not have any memory allocated and if it goes in this function set this textfield as first responder.

Related

Textfields don't get selected after reloadData on UITableView

In my UITableView I have rows which contain 4 textfields each. After user ends editing couple of things in UITableView datasource are being recalculated, so I need to reload UITableView after edition of textfield. But because tableview is being reloaded, when user touches another textfield to edit while editing the first one, second textfield doesn't get selected. He has to touch it again before it can be edited.
Is there a way to avoid this behaviour? Unfortunately I do need to use reloadData instead of reloadSections or reloadRows methods. Any help will be appreciated, thanks!
Reloading a table view will resign the first responder of the text field or even text view. You could try this trick if you have multiple changes that need to be made.
[[self tableView] beginUpdates]
[[self tableView] endUpdates]
Try assigning a gestureRecognizer to each field or same one to all fields. Once you capture the touch in required field, capture the tag and make that field the firstResponder.
Summing up all the help I received and my own ideas I've gone with the following:
-Delay reload for 0.01 sec so textFieldDidBeginEditing gets called
-Then save textfield's tag and cell's index path to variables in textFieldDidBeginEditing
-Call reloadData on tableview
-Call becomeFirstResponder on textfield saved with variables
This however caused keyboard hiding for a brief moment as it didn't have any responder during reloading. To resolve that I created a fake textfield in tableview's superview and assigned him as a responder up to a moment when tableview was reloaded to assign back to the textfield that was selected by a user.
Certainly not the cleanest solution, but it works.

UITextField Hide Keyboard But Reamin First Responder?

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.

Bring up keyboard when cell pressed?

Is there a way to bring up the keyboard when I click on a cell in a UITableView?
I tried to find some way to connect one of the outlets of a cell to a declared IBAction but that doesn't seem to work.
Thanks!
Yes, there is a way to do it but its somewhat complex:
you put a UITextField in your cell, but position it out of view
when you want the keyboard to appear, to edit some other UITextField in the cell, you make the UITextField that is in the cell offscreen the firstResponder
now the tricky part - as the delegate messages start flowing - you essentially set the UITextField you WANT to have edited the first responder
you get a flurry of delegate messages while this happens, so you need to do some work
In the end, the UITextField you want to get edited is the first responder. So, you can do what you want, but you have to spend some time working it all out.

TextDelegate not recognizing a textfield after hidden = NO

I have a textfield that is hidden to start with and when the user touches a UISwitch the textfield.hidden= NO, however, the -(void)textFieldDidBeginEditing:(UITextField *)textField doesn't kick in? any ideas
thanks
textFieldDidBeginEditing will get called when the textField becomes the first responder. If the user sets focus in the field to start changing it's content, it becomes the first responder. It will not get called when the enabled state gets toggled. If it's hidden, the user will not be able to edit it's content.
See:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html
This method notifies the delegate that the specified text field just
became the first responder. You can use this method to update your
delegate’s state information. For example, you might use this method
to show overlay views that should be visible while editing.
Implementation of this method by the delegate is optional.

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.