which delegate method will call click on back space button in keyboard - iphone

I have text field in that i have to some functionality click on back space button in keyboard when the text field is empty ...shouldChangeCharactersInRange is calling when the textfield has some text.how call solve this problem.

You can catch that key in
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
It is not written explicitly in the docs but if you press the backspace key then the last parameter string will have a length of 0.
At least this worked for me when using UITextView objects.
Hope this helps :)

You should read the documentation of UITextField... By the way here it is from the documentation of UITextField
textFieldShouldClear:
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

If the text field is or becomes empty, put a space character in it. If you detect a backspace, and the dummy space character is the only thing in the text field, don't delete it (yet). When finally reading out the contents of the text field, remove the spurious leading space if you added one.

Related

Detecting whether the user has typed in a UITextField

I know that if you use the following line of code, you can detect what the user has typed in a TEXT VIEW.
if ([[textView text] isEqualToString:#"")
I want to detect whether the user has typed anything in a text FIELD, and this does not work with text fields. What should I change? Thanks for your help!
The UITextFieldDelegate is the preferred way to find this out.
- (void)textFieldDidBeginEditing:(UITextField *)textField - this will only tell you that it has become the first responder / key field, this does not guarantee that the user modified the value
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - this method will tell you when ever the user hits a key to change the text, a "paste" will cause multiple characters to be in the string, backspace over one, or delete of a selection will cause the string to be empty, …
- (void)textFieldDidEndEditing:(UITextField *)textField - this is a common location to check the value in the field against the value in the model to see if an edit has occurred. this will only be called when the field relinquishes "key" status, note that a user could tap a button to cause the whole view to go away before this is called, often making it useful to track if any field you are interested in becomes the "key field" (see first method)
it can be useful to set break points in relevant implementations to familiarize yourself with their call order/logic
You can retrieve contents of a UITextField the same way you can a UITextView, by accessing the "text" property. The code below is if a text field is not empty.
if (![someTextField.text isEqualToString:#""])
{
NSLog(#"someTextField is not empty");
}
You can also use the UITextField delegate to detect when a user starts typing, stops typing, etc. More information about that is available in the documentation.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/doc/uid/TP40006991
Finally, here's a link to the UITextField docs.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/doc/uid/TP40006888
you can check for text length instead of content
if ([[textView text] length]==0)

UITextField SecureTextEntry field changes the keypad from numberpad to generic keypad

I have a textField created in IB. I have set the keypad type to Numeric Pad in IB.
When i make secureTextEntry = YES in -(void)textFieldDidBeginEditing:(UITextField *)textField method , my keypad changes from numberpad to generic keypad. I even tried to make the keypad to be numeric programatically but still it doesnot changes.
I have set it like this
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.activationCodeTextField){
self.activationCodeTextField.secureTextEntry = YES;
self.activationCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
}
}
FYI,
I cannot set the textField to be secure in IB because i have an hint text like "4-digits" displayed to the user in the textfield till he starts typing in the text filed. Once he starts typing in the textfield, i want the entries to be a secure text so that it displays only * instead of actual characters he types.
Can you please let me know whats wrong in what I am doing?
I also have two more query
I have a textField where i have some default text (like hint text). I want this hint text to be displayed to the user till the moment he starts typing. I dont want to clear this text when the user clicks on the textfield and then the default text clears away. but, i want this text to be displayed till the moment he actually starts to type something on the keypad, then the default must be cleared and then the actual typed in text to be displayed on the textfield. IS it possible to acheive this ?
Is it possible to set the cursor position of textfield to the first character programatically. This is needed because, i have some default text (hint text) and the cursor is at end of the text. I want the cursor to be at the start of the text. How to make this possible programatically?
Have you tried using -setPlaceholder: on your UITextField? That way you wouldn't need to do put text in the text field, and then later manually convert it to be secure. e.g.
- (void)viewDidLoad {
...
[textField setSecureTextEntry:YES];
[textField setPlaceholder:#"4-digits"];
...
}
That will completely take care of your last two questions. Regarding the first, though, I'm not sure if you can have a numeric keyboard for a secure UITextField or not. It would seem that you can't if setting it programmatically has no effect.

UITextField > force cursor to stay at the end of the field

The user should not be able to move the cursor of a UITextField somwhere else in the entered string.
I need this because I want the user to enter a currency amount. This is done by letting the user enter numbers which will be added to the end of the amount and the commata will be moved to the third position from the end.
How do I force the cursor in an UITextField to stay at the end of an entered string?
Place a UIButton over your UITextField. Add an IBAction to the button and have it call UITextField.becomeFirstResponder().
This prevents clicks from passing, cursors for being able to be modified, but still allows the user to click the view and select it.
I solved the problem by hiding the original entry field and show the value formatted in a label. This prevents the user from changing the cursors position.
Create a subclass of UITextField with
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return NO; }
to disable all gestures on that field. This implies that you make that field the firstResponder programmatically though.
I think you can use:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Just give him the correct range.

UITextField show " € " symbol

I have a UITextField only allow users to enter the number, which is amount of money. when user types 10000, the textfield will show € 10.000. And it will change dynamically when user types or backspace in the textfield. How do I implement it in iPhone?
You'd want to give the UITextField a delegate and implement something for
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
in that delegate that checked to see if the field's format was valid and altered the text if so, inserting the characters in the selected location, inserting / removing the . / currency symbol, then returning NO.
The tricky part is that the insertion point jumps to the end of the field whenever you replace the field's text - there are some undocumented APIs to retrieve / set its position, but not any official ones. Not a problem in most cases, since a user isn't very likely to move the insertion point in a field this small, but you might get a few complaints.

Limit number of characters in UITextField where input is from UI

I have a UITextField for which I get input from some buttons that are part of the UI (so the input does not come form the phone's virtual keyboard). I want to limit that number of characters. I tried using the shouldChangeCharactersInRange, but it only works if the input comes form the virtual keyboard, and it doesn't work if the input comes from the buttons on the UI. Is there anyway I can solve this without programmatically having to count the characters in the text field every time I want to update it?
Thank you,
Mihai Fonoage
Implement the UITextField Delegate method shouldChangeCharactersInRange:
set the method to return YES when the length of the string is less than your maximum count.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length<3) {
return YES;
}
return NO;
}
Since you are programmatically making changes to the UITextField once the user clicks the relevant buttons on the UI, you should keep track of the number of characters entered using a counter.
Increase the value of the counter every time a relevant UI button is touched. Update the textfield only if the counter value is <= maximum allowed no. of characters.
Check the length of the string property on the textField before accepting changes.
There is a textRectForBounds Property