I have a UITextView which I want to call some code after leaving it. How I can do this?
Thanks.
The UITextFieldDelegate protocol has a textFieldShouldEndEditing: method. This method will be called before the text field resigns the first responder status. For example when another control is selected.
This is a good place to put code that needs to run after the user is done working in the text field.
You can also prevent the user from leaving the text field by returning NO from this method. This is useful when you for example want to show an error message or error marker next to the field.
Related
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.
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.
Hi I am wondering if you are able to use the ios keyboard to update a textfield as the user types?
you could get each character as they type through UITextFieldDelegate delegate methods,
Once you get the set of character through delegate method , you could update you UILabel.
If you're talking about UITextField, then yes, naturally, those are meant to be modified by the user and interact with the keyboard just fine. If you're talking about UILabel, then no, the user can't type directly into a label, but as #Jhaliya suggests you could update the label whenever the user changes a text field, text view, or other object that can interact with the keyboard.
I'm adding some user feedback mechanism into my app. The user types some comments into a text field and when that editing is done it updates a UITextView. Then when the user hits the submit button and moves on in the app the user may have need to send more feedback from the same form for a different item. I can reset the other fields and labels in the app to their default values when I hide the view, but not the textField (?). How can I reestablish the placeholder text next time the user accesses this view?
Your suggestions are humbly appreciated.
EDIT:
Thanks to Dwaine. Apparently I had the [textField setText:nil]; in the wrong place. Placing it in my textFieldDidEndEditing worked fine. Also, I was using the Did End on Exit rather than Editing Did End in Interface Builder which screwed things up.
In either the ViewWillAppear or ViewDidAppear (I'd suggest ViewWillAppear) function, set the text value of the UITextField to...nil I think...or just an empty string...
That should make the Placeholder Text show up again I think ^^
I have a UITable View with a textfield that is editable right on the view (like Phone in contacts, etc.). I want to enable/disable my save button conditional up text being present in this field. So, I want the button to start out as disabled (for a new record) and then, as soon as I type the first letter into my text field, I want the button enabled. If I delete again back to zero, I would like the button disabled. You get the point.
Now, for doing this I need some way to detect the text being inputed while the user writes it (and when he finishes editing).
Does anybody know how to do this?
Thanks a lot. Still noob...
Try this: (from the Apple documentation for UITextInputTraits)
enablesReturnKeyAutomatically
A Boolean value indicating whether the return key is automatically enabled when text is entered by the user.
#property(nonatomic) BOOL enablesReturnKeyAutomatically
Discussion
The default value for this property is NO. If you set it to YES, the keyboard disables the return key when the text entry area contains no text. As soon as the user enters any text, the return key is automatically enabled.
Have your view controller adopt the UITextFieldDelegate protocol, and then implement a couple of the protocol's methods:
– textFieldDidBeginEditing:
– textFieldDidEndEditing:
Also, be sure to set the text field's delegate property to point to your view controller. The text field will then automatically send these messages to the controller when editing session begins and ends.