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

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.

Related

Restrict the TAB key on accessory keyboards

I have a UIScrollView with 2 views, side by side, each of which covers the entire screen.
They are moved to visible bounds on user's action, only one covering the screen at a time. Both of these views have multiple UITextFields. Working with the simulator, I fill in a textField in the first view and when I press the Tab key, the firstResponder is assigned to a textField in the other view. I understand that on using the device, the user will not be able to do that. But what if the user uses a bluetooth keyboard, or similar accessory? I do not want a textField, that is currently not visible to become firstResponder. Can this be done?
EDIT: I just remembered the canBecomeFirstResponder method. But how do I determine which textField is about to becomeFirstResponder?
It sounds like the problem isn't that they shouldn't be able to tab between the two text fields, but instead that they shouldn't be able to edit a text field that isn't visible, and they should be able to tab between them if they are both visible at the same time.
Instead of restricting tab, I would implement the UITextField delegate method -textFieldShouldBeginEditing:, which allows you to return a boolean whether or not that text field should become the first responder.
Something such as:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Only edit if the text field is visible
return !textField.isHidden;
}
You may need to adjust this code to fit your 'is visible' status of the text field.

Closing UITableViewController when the keyboard covers cells causes crash

I have a UITableViewController with custom cells for static and editable text. Standard stuff, nothing fancy.
The cell data is stored so that when cells are reused, the data is placed back inside them.
The problem is this: when the user has the cursor in a UITextField cell and the keyboard's up, the keyboard is covering three rows in the table view. There's a cancel button at the top of the view controller which is supposed to dismiss the view controller. When I call dismissModalViewController, the keyboard hides, but the UITableViewController wants to refresh the rows that were under the keyboard while the view's going away. However, the view's already in its death throes and I get a crash because the table view's trying to call cellForRowAtIndexPath to update rows that are in the process of being removed.
I tried waiting for the keyboard to hide. No good.
I tried resigning the first responder so the keyboard goes away and updates the rows first before dismissing the view controller, but there's nothing I can find that tells me the data's finished updating.
What I need is some event that tells me that the table is finished updating, or tell the table view to not ask for data from the delegate, or tell the table view to not update when the keyboard is going away.
Or there's something simple I'm missing.
Any help?
If you have not more cells, i advice you to make a different Reusable Identifier for each cell.That very better to save the state of cells after being dequeued
I solved my own problem. Inside cellForRowAtIndexPath, I was setting text inside a UITextField for pointers that may no longer exist. What I did to fix the problem was still create the cell, but before setting the text inside it, I checked if the Cancel button was pressed and if it was, return the empty cell.
I was hoping there'd be a better solution such as waiting for the cells to finish updating, but that doesn't seem to be happening.

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.

UITextField becomeFirstResponder works only once

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.

How to display a text field hidden by virtual keyboard?

I've layed out a view with some labels, a button and especially a text field in the bottom of this view. The issue is when the text field gets the focus, the iphone virtual keyboard hides the text field, so we can't see what we're typing (and I can't move the text field to another part without breaking this layout)...Any idea on how to fix this issue ?
Thx for helping,
Stephane
There is a method of textFieldDelegate calld
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
When this method gets called you can change frame property of you UIView and shift it upward.
Same way when textFieldShouldEndEditing gets called you can shift view down again.
Moreover, listing to notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification can also be useful to trigger view shifing.
If you do not know how to slide view see this.
http://iosdevelopertips.com/user-interface/sliding-views-on-and-off-screen-reader-contributions.html