I need to validate two uitextfields differently. How do I do this? - ios

One text field only takes alphabet characters and the other one is alphanumeric. I have defined shouldChangeCharactersInRange in my ViewController for one of them. Now I don't know where I'm supposed to define the logic for other uitextfield.
Can someone help me understand how this should work?

If they both have the same delegate, then they would both be validated in the same shouldChangeCharactersInRange method. You need to put an if-else clause in that method to check which text field is the sender. You need IBOutlets for the two text fields so you can do the comparison.

You have a couple options. If you have created iVars for your text fields, checking which one is calling shouldChangeCharactersInRange is as simple as ==. Below I've also shown that another option would be to assign tags to the text fields and check the tag of the sending text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Your first option
if (textField == myFirstTextField) {
//
}else{
//
}
//another option, if you don't want to create iVars you can assign tags to your text fields and do this
if (textField.tag == 99) {
//
}else{
//
}
}

Related

IOS: textField now formatting entry as a phone number throughout app

I used the method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
to format some phone numbers that are typed into a textfield.
I have now noticed that my login screen which uses textFields has suddenly begun formatting the userid and password as phone numbers as well making it impossible to log in.
Can I infer from this that if you use the above method, it affects all textFields in your app?
If so, is there a workaround to only use it for phone numbers?
Thanks for any suggestions
I have 2 suggestions.
Use textFieldShouldBeginEditing to set a class var, here called "activeTextField", then in shouldChangeCharactersInRange compare with your text field
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
activeTextField = textField
return true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if (textField == textPhono) {
//do your logic
}
}
Mark all of your phone text fields with especific tag for phones, then compare tag in "shouldChangeCharactersInRange"

UITextField hide all characters

Our customers want a Textfield for a Password which is not showing any character while typing.
Is there any Setting in iOS to activate this behaviour or is it possible to hide also the last typed character using the secureTextEntry property on a UITextField?
You can set your view controller as delegate for your text field, and add the following method, this will not allow the text field to show that last character:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.pinTextField) {
NSString *currentText = textField.text;
NSString *newText = [currentText stringByReplacingCharactersInRange:range withString:string];
textField.text = newText;
return NO;
}
return YES;
}
please check below image attachement.
When you put uitextfield in your storyboard or xib. then after select that uitextfield & you can see the properties list in your right side of xcode. Now you can see the second image type of propeties in your properties list. Now true check box for secure text entry. when you select it then after you can see the first image type of entries in you app.

how to check if focus switches from one text view to another

I've got 2 UITextFields. I can detect when the user in inside either of them with:
textFieldDidBeginEditing:(UITextField *)textField
Which works great. But i want the textfields to animate out when the user is not inside either of them. Currently I'm calling: (void)textFieldDidEndEditing:(UITextField *)textField
However, this is called even if i switch from on textfield to the other. Is there a better way to call do this?
What I would do is add a BOOL flag to detect if you should animate out your text fields or not. It would work something like this:
-(void)hideTextFields {
if (self.shouldHideTextFields) {
self.textField1.hidden = YES; // Or whatever you want to do with
self.textField2.hidden = YES; // your text fields
}
}
Declare a method that checks the BOOL flag and decides whether to hide or not the text fields
Whenever either of the textFieldDidBeginEditing:(UITextField *)textField methods are called set the BOOL flag (you can call it 'shouldHideTextFields') to NO.
Whenever either of the textFieldDidEndEditing:(UITextField *)textField methods are called set the BOOL flag to 'YES'. Also, call [self performSelector:#selector(hideTextFields) withObject:nil afterDelay:1]; to give the user a little time to select the other text field. If he/she does, the flag will be set to NO thanks to the above step.
Hope this helps!
On didend check if either of your textviews is currently the first responder with [textfield isFirstResponder] or [textfield isEditing]. There may be a slight delay when one ends and the other takes control. If that's the case then you could do this check after a slight delay using performSelector:afterDelay.
On textFieldDidEndEditing delegate method, do not just perform your disappearing animations, but do that in dispatch_async, checking if there is no textField editing right now.
In case if user just ended editing of one text field, there will be no editing textfield. But if user had switched to another textfield, it will already start editing and it can be easily checked by isFirstResponder method.

one TextField to 6 others

Quick Question, I hope,
I have a UITextFeild that I want to type in an number and have that number populate into 6 other UITextfields on the same VC.
The first textfiled is called percentage goal while the others are named endmonth1year1percentage, endmonth2year1percentage, endmonth3year1percentage, etc.
I am currently using iOS6 with storey board.
Any help is appreciated. Thanks.
Detect the change in the first text field: UITextField text change event
And then update the text property of other text fields you want to be populated.
try like this,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//replace your textfield names here
textField1.text=textField.text;
textField2.text=textField.text;
textField3.text=textField.text;
textField4.text=textField.text;
textField5.text=textField.text;
return YES;
}
So for the first UITextField you must set the delegate the class that is responsible with the display of the text fields (a view controller or a custom view). For the other UITextFileds you should set a tag like 1,2,3..for each UITextFiled (because you say that will be lots of UITextFields)
After you set the delegate for the first UITextField and setup the tags, you can implement two different delegate methods (depending on what you want).
The method that Sunny provided, this is for instant changes:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
return YES;
}
Second, you can use the following if you want to update the textfields only after the user finished typing and the keyboard is hidden:
-(void)textFieldDidEndEditing:(UITextField *)textField {
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
}
EDIT
So first of all delegate is a pattern heavy used by iOS here is a tutorial that will explain the basic concept of delegate.
Second, some of the UI controls that iOS provide have a delegate instance (after you read the above tutorial you will understand why and how it's working). A class can be a delegate of a custom UI control only if the class implements the required methods that the delegate provides (NOTE: there are optional methods also in the delegate), if the class doesn't implement the required methods a build warning will be shown at the line where the delegate is set.
Third, the method used in this answer are delegate methods of the UITextFiled (check apple docs)
I lost count, [tag][3] is a property available for all UIViews of subclasses of UIView that can be used to identify an object, but be careful that this property by default is 0 so make sure when you set the tag property you will use a value > 0.
For more details please use google and Apple Docs

What does the return value mean for the UI TextField Delegate method textFieldShouldReturn:?

The apple docs offer:
Asks the delegate if the text field should process the pressing of the
return button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Parameters textField The text field whose return button was pressed.
Return Value YES if the text field should implement its default
behavior for the return button; otherwise, NO.
Discussion The text field callsthis method whenever the user tapsthe
return button. You can use this method to implement any custom
behavior when the button is tapped.
My question is what does the return value do? I have been implementing the behavior in this method so it makes no difference what is returned. Is this not the correct method to perform the action?
For instance, if I implement a search function, should I trigger the search action in this method or somewhere else.
This is the correct method to trigger an action when the user taps the Return keyboard key (whatever it happens to be labeled).
The return value from the textFieldShouldReturn: delegate method almost never matters. If you are dealing with a single text field then it definitely doesn't matter.
I ran into one issue a while back that made me realize that just under the right situation, the return value does matter. I had a screen with several text fields and then a text view. I was using this text field delegate method to change the first responder from text field to text field to text view. I found that if I returned YES in this delegate method and then made the text view the first responder, the newline was being sent to the text view.
As a result of this, I now always return NO from this delegate method to be safe.
When you press the return button on the keyboard the textFieldShouldReturn is called.
I never experienced any difference between the return value.
Customization example:
If you have two textFields when the user presses return button from first textField, you can give focus to second field in the following way:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn:");
if (textField.tag == 1)
{
UITextField *passwordTextField = (UITextField *)[self.view viewWithTag:2];
[passwordTextField becomeFirstResponder];
}
else
{
[textField resignFirstResponder];
}
return YES;
}
So you can use this delegate method for triggering search functionality.

Resources