Action when the small cross button of UITextField is pressed - ios

I can add a small cross button that is used to clear all the text in a single click in a UITextField with the following code.
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
I also implemented UITextFieldDelegate.
I want to activate/deactivate the right bar button of the UINavigationBar according to the changes in my UITextField. The rule is simple: if the text field has at least one character, just enable the button, else disable it.
I implemented the UITextField's delegate method textField:shouldChangeCharactersInRange: to constantly check the requirement and update the status of the button.
Now my problem is, when I click the small cross button on the text field, the method textField:shouldChangeCharactersInRange: is not called. So the required action is missing. How can I identify the hit event of the clear button?
Some pictures to show what I'm talking about.
Initial stage: here, the "Done" button is disabled because there are no letters in the text field
When typing a letter, the "Done" button is enabled
Now, if I click on the clear button, the "Done" button is still enabled. This is wrong.

Use the delegate method textViewShouldClear::
Asks the delegate if the text field’s current contents should be
removed.
- (BOOL)textFieldShouldClear:(UITextField *)textField
Parameters
textField
The text field containing the text.
Return Value
YES if the text field’s contents should be cleared; otherwise, NO.
Discussion
The text field calls this method in response to the user pressing the
built-in clear button. (This button is not shown by default but can be
enabled by changing the value in the clearButtonMode property of the
text field.) This method is also called when editing begins and the
clearsOnBeginEditing property of the text field is set to YES.
Implementation of this method by the delegate is optional. If it is
not present, the text is cleared as if this method had returned YES.
Availability
* Available in iOS 2.0 and later.
Declared In
UITextField.h

You can use textFieldShouldClear: delegate method to handle when user taps on clear button.

Related

iOS Custom keyboard can't return focus to app's textfield

I'm working on a custom keyboard for iOS which will have its own search field (similarly implemented by PopKey).
My keyboard's textfield is able to take the focus with becomeFirstResponder and I'm able to give it up by using resignFirstResponder. However after I resign focus, the host app has a difficult time retaking focus despite touching the form. The app's textfield will still show the text cursor blinking.
Any ideas? Thanks
The solution is a hack, as of right now you can't really give the host app its focus back.
Subclass a UITextField and on its delegate implement
textFieldShouldBeginEditing by returning NO.
Add a BOOL property isSelected that gets set to YES in touchesBegan (not to be confused with the default selected property)
In your keyboard's keyPressed method, if searchField.isSelected, manipulate the searchField.text. Else, manipulate textDocumentProxy like normal.
Add a clear button and method that wipes searchField.text and searchField.isSelected, allowing any further keystrokes to return to the textDocumentProxy
Add an animation that replicates the blinking type cursor

ios/xcode/objective c: Capture last keystroke in textfield

I have a method to enable the save button that I want to fire when the user meets several conditions, for example, username is at least 4 characters. To test these conditions I need to check with every keystroke. What is best touch event (through storyboard) to capture this?
Have tried "Editing did end" however, that doesn't fire until you click out of textfield. I'd rather see button enabled as soon as textfield has acceptable entry. Also tried on touchup inside and value changed but neither seem to work.
What is best touch event so that as soon as user types fourth character, button is enabled?
Here is action method that textfield is wired to. Note it does not say anything about touch event which is present in storyboard.
- (IBAction)changedText:(id)sender {
[self updateSaveButton];
-(void) updateSaveButton
{
NSString *name = self.username.text;
NSLog(#"update save button called");
self.submitButton.enabled = (name.length > 3);
}
What I do is to assign the text field a delegate. In this case, the delegate would whatever is the instance whose class's code you have shown in your question.
The delegate is sent a bunch of delegate messages, documented in UITextFieldDelegate. In this case, the one I would use is textField:shouldChangeCharactersInRange:replacementString:, which fires every time the user does anything that proposed to change the text in the field.
Simply respond as you see fit, and return YES to allow the change to take place.
Alternatively, you can treat the text field as a control (UIControl) and use the text field's Editing Changed control event to set up a target-action pair. But in actual practice I always end up using the delegate method instead.

How do I make it so next button can't be pressed on keyboard without text?

Working on an app and wondering how to make it so the next UIButton on the UIKeyboard cannot be pressed unless text is entered in the UITextfield?
Checkmark "Auto-enable Return Key"
You should be implementing textFieldShouldReturn in you UITextField Delegate. There you can check whether a valid string has been entered into the text box and allow the next button or not.
Documentation here

disable ipad default resign button Xcode

I am working on iPad app and for textfield i have some validation like its text cant be empty if so it should display an error alert.But in iPad we have its default button at the bottom right corner of keypad which has image some what like a keyboard.Clicking on this very button the keyboard resigns automatically without any validation check
.
How i can disable the textfield resign clicking on this button.
[textField resignFirstresponder];
or
textfieldsholdreturn method will help ?
I'm not 100% sure about this, but the textFieldShouldEndEditing: method in UITextFieldDelegate protocol should be what you're searching for.
The documentation says:
Note: If you use this method to validate the contents of the text field, you might also want to provide feedback to that effect using an overlay view. For example, you could temporarily display a small icon indicating the text was invalid and needs to be corrected. For more information about adding overlays to text fields, see the methods of UITextField.

Keep keyboard always on top & visible

I have view with a text field, an image and a few buttons.
I want to make sure the keyboard is displayed and is on top when the view is displayed
AND
I want to make sure it doesn't go away after I type something in to the text field and submit it.
I called [txtField becomeFirstResponder] with viewdidload and the keyboard is appearing by default but with a tiny delay after the view is displayed.
Also the becomefirstresponder doesn't help after I have my text field submitted.
Thanks in advance for your help!
Also the becomefirstresponder doesn't help after I have my text field submitted.
That part makes no sense. By default, a text field does not dismiss the keyboard unless you dismiss it with endEditing: or resignFirstResponder. If the keyboard is going away, you must be making it go away. So don't and it won't.
EDIT: And indeed, your comment later reveals the answer: you've hooked up the didEndOnExit control event from the text field. Well, that causes the keyboard to be dismissed when the user presses the Done button! So you are effectively hitting yourself in the face and then complaining that someone is hitting you in the face.
So the solution, obviously, is don't hook up the didEndOnExit control event (to anything). Instead, just give the text field a delegate and use the delegate messages to learn what the user is doing. None of those have any automatic behavior with regard to the keyboard, so the keyboard won't be dismissed automatically. For example, to learn when the user is typing, use textField:shouldChangeCharactersInRange:replacementString:. To learn when the user has hit the Done button, use textFieldShouldReturn:. And so on.

Resources