Check if textfields were edited? - iphone

I have a UITextField in each cell of my tableview. I want to set a BOOL for if any text was edited in any textfield so I can prompt user to save later on.

One simple approach is to set a tag for each of your UITextFields, and have a BOOL array with indexes matching the tags, named editedTextFields. Then, implement the method from the UITextFieldDelegate protocol:
- (void)textFieldDidBeginEditing:(UITextField *)textField
Inside of it, using textField.tag to find out which UITextField has been edited, you can then set the BOOL array at the equivalent position to YES.

Use UITextFieldDelegate and implements :
- (void)textFieldDidEndEditing:(UITextField *)textField

You can try UITextField's delegate solve this problem. Have a try.

Related

UITextField with trailing placeholder text

I am making an UITextField input where user can enter an subdomain for their site. What i want to accomplish is, that i can show the whole url in the input but only the subdomain part is editable. Any tips how to accomplish that?
Update
The right solutions was to use UILabel in UITextField's rightView and calculating the position in rightViewRectForBounds.
First, UITextField has a property named AttributePlaceholder, it allows to display different color of placeholder.
And then when didBeginEditing:, fill the textfield with url, without the subdomain. Remember the count of characters.
When user editing, called on the method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
Return NO when textfield.text.count <= characters.count

How to know which tableview cell's text field is changed its content

I have a tableview with around 100 text fields inside it. I added only one text field. It is reused and it is accessed using tag.
The problem is this: suppose the user has changed some textfield. Now, I want to access that text field changes during saving of the data.
How can I do this?
You are very near to your goal. You have already assigned tag to your UITextField. You can use and track tag in this delegate method.
-(void)textFieldDidEndEditing:(UITextField *)textField
{
}
First set tag of textfield with indexpath.row
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// replace text in your array
}

UITextField cursor back movement

I want to prevent cursor back movement in UITextField. or I need to prevent entering text between two characters which is already entered.
How can I do this?
There is no property or method to prevent the user from using the magnifying glass on a particular text field. You can try to block all the characters inside the string:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(range.location < textField.text.length) return NO; else return YES;
}
After some research, I found this question: Disable Magnifying Glass in UITextField. The author of the accepted answer says there's a way to do it:
You can also set userInteractionEnabled to NO so that the user can't
tap the field. Call becomeFirstResponder manually so that the field
gets focus since the user can't tap to focus.
But, as I found out, it doesn't work, since setUserInteractionEnabled:NO prevents the text field from becoming the first responder. After the second research I found another question: Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO. MaxGabriel user claims he found the way to do it:
What I do is add that hidden text field to the view, and call
becomeFirstResponder on it. The user has no idea this text field
exists. In the delegate callback from the text field, I take the text
the user typed in and add it to a UITextView (though you could add the
text to whatever you wanted, like a UITextField like in your
question). I turn off userInteractionEnabled for the visible text
view. This creates the effect you desire.
Stack Overflow is a huge database, a little bit of research before asking a question may help you in the future.
For stopping entering text in UiTextField, you should use uitextfield delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Set tag for a textfield
if(textField.tag==10)
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength < [textField.text length]) ? NO : YES;
}
}
Modify the logic as per your requirement.
For preventing entering text between two characters Use NSRange and textfield text of range is less than textfield text length then return No then text will not be entered in UITextField
You cannot prohibit caret moving, but you can use UITextFieldDelegate's method textField:shouldChangeCharactersInRange:replacementString: for testing if text was typed inside already typed string, if it's not then change text in textfield manually and move caret to the end with the help of selectedTextRange property of UITextInput protocol.

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.

Compare multiple strings in textview of iphone

I want to compare all the strings entered in a textview of iphone. Once the user finishes with typing in textview I want to compare strings entered by the user.
How do I do it?
Please help
I am not sure what you define "finishes with typing", here is my guess:
You have to set up your class become UITextViewDelegate and implements some methods:
If you want to get the text whenever the text changes, use:
- (void)textViewDidChange:(UITextView *)textView
If you want to get the text whenever the text view resign first responder, use:
- (void)textViewDidEndEditing:(UITextView *)textView
I also don't know what you define by comparing all the NSString. One way to do is put them into NSSet and NSSet itself will remove duplicates of NSString for you