saving text data from UITextField without pressing additional key (help needed) - iphone

I am new to iPhone programming. I am working with UITextField and UITextFieldDelegate. I have five textfields. I am trying to input data in them using numberPad keyboard. And the data should be 1 digit long. After that hitting another key should take it to the next textField. I have implemented it all but the only problem is when all 5 textfields are filled, I have to press keyboard 6th time to save the text data of fifth textField, Is there any way I could save the text data of 5th textfield without pressing any key on the keyboard 6th time?
Any help or suggestions are welcomed.
Thanks
Vik

You can use textFieldDidChange delegate Method for 5th TextField and compare it like this.
if (textField==textField5 && [textField length]>0){
[textField resignFirstResponder];
}
I hope this will help.

Im assuming that you are using UITextFieldDelegate method to move to next text field. You can keep a count on how many text fields are entered and keep checking it:
if(count == num_of_textfields)
{
[textField resignFirstResponder];
//Call your method to process the input here.
}

Related

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
}

How i can fetch what is typed between 2 UITextViews on iphone?

i have two UITextView items, how can i fetch what is written using a button on iphone?
Imagine something like a translate app, the user enters a word in UITextView 1 and by pressing the button the UITextView 2 is getting filled with data.
UITextView has a property text. Simply use this.
Set up IBOutlets for textView1 and textView2. Then have the button do something along these lines:
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:textView1.text];
}
To get fancier, you can have a method -(NSString *)transformText:(NSString *)text that translates or does whatever you like. Then use
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:[self transformText:textView1.text]];
}
Create an IBAction method that is linked to a button and in that method read the "text" property of the textView or textField, do your calculations on it and assign the results to the text property of thee second field.

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.

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