objective-c how to save textfield values through textFieldShouldEndEditing: - ios

I've a UITableViewController which has Save button. At present I'm displaying row's of UITableViewCell's. UITableViewCell contains textfield . Let's say if user enters text in 1st table cell and hits "Save" then I'm losing the entered/modified text.
suppose I have a textField
((UITextField*)[cell viewWithTag:101]).placeholder = #"title";
((UITextField*)[cell viewWithTag:101]).text = arrayActivities[indexPath.row][#"title"]; //arrayActivities[indexPath.row];
((UITextField*)[cell viewWithTag:101]).keyboardType = UIKeyboardTypeDefault;
how I would save arrayActivites editable values??

Have you tried making the UITableViewController a UITextViewDelegate? So in your tableView's .h file:
UITableViewController <UITextFieldDelegate>
and in your .m file you should be able to call:
- (void)textFieldDidEndEditing:(UITextField *)textField {
[array addObject: textfield.text];
}

Keep one global dictionary dctGlobal in your class.
Set textfield.delegate=self in cellForRowAtIndexPath method.
And then get and save value for text field in following method
- (void)textFieldDidEndEditing:(UITextField *)textField
[dctGlobal setObject:textField.text forKey: textField.placeholder];
On save, Use dctGlobal for saving into database.
Thanks!

Related

Send dynamically created textfield data in array

I have dynamically created UITextfield using for loop and now i need to send the text contained in the UITextfield to an array inside button click and send back to another viewcontroller. But when I pass the data in array, then textfield data is sent blank. Please help.
Thanks.
To do this you have to use delegate method.
For Ex:
Initialize the Values array before calling this method;
for(int i=0;i<5;i++)
{
UITextField *text1 =[UITextField alloc]init];
// set frame for text1
text1.tag = i;
text1.delegate=self;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[Values setObject:textField.text atIndexedSubscript:textField.tag];
}
on passing array dont forget to add
[self.view endEditing:YES];
Please let me know if you have any help

How to incorporate textfield change notification into textFieldDidEndEditing

I have a UITableView with custom UITableViewCells and each tableview cell has a UITextField. By default the textfield has a title already in place that can be edited by the user. The default title in the textfield is associated with a file in NSFileManager and when the user finishes editing the text field and taps "return", a method that changes the file name to what the user enters gets called. This works fine, but when the user taps the textfield but doesn't do any editing and then taps "back" to go to the previous view controller, I get a warning from NSFileManager saying the file name already exists. This doesn't cause any problems, but its annoying. I know the method that calls NSFileManager to change the file name shouldn't get called unless the user edits the textfield, but I'm not sure of the best way to implement this.
I saw this post, but wasn't sure how to incorporate it into what I'm doing:
UITextField text change event
I was wondering if someone could give me some tips on how to make this work.
-(void) textFieldDidEndEditing:(UITextField *)textField
{
textField.delegate = self;
NSArray* cells = [self.audioTable visibleCells];
for (OSAudioTableCell* cell in cells)
{
if (textField == cell.textField)
{
NSInteger index = cell.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
Recording * recording = [self.fetchCon objectAtIndexPath:indexPath];
NSString * previousPath = recording.audioURL;
//I left a lot out, but this is where I call the method to change the file name
NSString * returnedURL = [self.managedDocument changeFileName:previousPath withNewComponent:textField.text error:&aError];
}
}
}
I would just check if the textField's text changed. If it did then go through the block you pasted above. If not, then just do nothing. You can do this by holding a temporary reference to your textfield's value before any edits happen:
// At the top of your class
#property (strong, nonatomic) NSString *currentFileName;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_currentFileName = textField.text;
}
Then in your method above, I would check if the two strings are not equal:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (![textField.text isEqualToString:currentFileName]) {
// continue with your logic
}
}
Try this. Add the delegate method -(void)textFieldDidBeginEditing:(UITextField *)textField. And in this method do something like:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
self.textBeforeEditing = textField.text;
}
And then, do a compare when textFieldDidEndEditing is invoked:
-(void) textFieldDidEndEditing:(UITextField *)textField {
...
if(![self.textBeforeEditing isEqualToString:textField.text]) {
// Change the file name
}
...
}
You could implement textFieldDidBeginEditing:, in which you would store the unedited value of the UITextField in an instance variable. Then, in textFieldDidEndEditing: simple compare the before and after values, and if they're different call your NSFileManager method like you normally would.
Example
#interface MyClass () {
#property (strong, nonatomic) NSString *originalText;
}
#implementation MyClass
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.originalText = textField.text;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([self.originalText isEqualToString:textField.text]) {
// Your original code here.
}
self.originalText = nil;
}
#end

UITextField clear when selected, but also restore data if nothing is input

I have a table with UITextFields, and I want to make it so that when a textfield is selected, the current info is cleared (to allow new input), but if the user decides (after selecting) that they don't want to change it, I want them to be able to click elsewhere and the data from before reappears in the textfield.
Is there an easy way to do this?
A good way to do this that's nice and user friendly is to use the placeholder property. Set up your viewController as the textfield's delegate (as described by Andeh) then add the following code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = textField.text;
textField.text = #"";
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.text.length == 0) {
textField.text = textField.placeholder;
}
textField.placeholder = #"";
}
And you're done. If you have multiple UITextFields on the page and you don't want this behaviour for all of them you can add a check to the methods above.
In the textFieldDidBeginEditing delegate method, save the current value to a persisting variable before clearing the UITextField. Then in the didFinishEditing delegate method, if the new length of the user input is 0 set the text back to the stored value!
UITextField Delegate docs for reference.
First I think you have two sets of behaviors here.
The text field must clear the value when you begin editing. This exists: -clearsOnBeginEditing.
The text field must restore the previous text if text is empty. Subclassing seems the better solution.
Here is a possible sample class:
// MyRestoringTextField.h
#interface MyRestoringTextField : UITextField
#end
// MyTextField.m
#interface MyRestoringTextField ()
#property (strong, nonatomic) NSString *previousText;
#end
#implementation MyRestoringTextField
- (BOOL)becomeFirstResponder
{
BOOL result = [super becomeFirstResponder];
self.previousText = self.text;
return result;
}
- (BOOL)resignFirstResponder
{
BOOL result = [super resignFirstResponder];
if (self.text.length == 0)
self.text = self.previousText;
return result;
}
#end
Hope that helps.
To clear and then restore a textField if you fail to make an entry, use the following delegates as such:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:kTextFieldIdentifier];
textField.text = #"";
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:#""])
textField.text = [[NSUserDefaults standardUserDefaults]
stringForKey:kTextFieldIdentifier];
return YES;
}
As of iOS 8.1, textFieldDidBeginEditing is already receiving a cleared text. You should use
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
to initialized the placeholder field.

how to dynamic fill the text field in ios app

I have a one page application with a couple of text fields. One field is a Drop-down menu, with which the user can choose the item to fill in this text field (text1). After this text field (text1) is filled, how can I automatically update other text fields (text2, text3, etc).
For example: I have three text fields, text1, text2, text3. On the backend, I have a matching array list between them, so the value from text1 will able to find the proper value for text2 and text3. But, how can I dynamically update text2 and text3 after the user changes the value of text1.
Another more basic question: how can I display the text string in text field in viewdidload?
For example: I have retrieved value from database, how to display this value in text field upon load of the view.
ok, I'll try to address the questions one by one:
first the displaying of a text field on load.
create a outlet to a label to hold your text.
example - start in .h file
#property (weak, nonatomic) IBOutlet UILabel *textbox1;
Now in Interface Builder, drag out a label and then connect the label you just dragged with the property you setup.
Now you have an outlet to store you value.
now to put something in it go to .m file in -(void)viewDidLoad add something like this:
self.textbox1.text = #"some text to display";
That should get something on the screen.
Now, if you want to dynamically update other on-screen labels either during or after the user is typing, you need to implement a text field delegate. Its a little complicated to try and explain the whole thing, but basically in your .h file your implementation line should look like this:
#interface YourViewControllerNameHere : UIViewController <UITextFieldDelegate>
then in your .m file you have a few changes to make
first in viewDidLoad add this line
self.textbox1.delegate = self; //this tells the UILabel to send changes to your program
then in some open space you can implement these callback methods:
then run your program and the logging will tell you when and where things are happening.
basically, you call your code from the correct method depending on what you want to do.
- (void)textDidChange:(id<UITextInput>)textInput {
NSLog(#"text did change");
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"text field did begin editing");
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"text field did end editing");
}
-(void)textWillChange:(id<UITextInput>)textInput {
NSLog(#"text will change");
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(#"text field should change characters in range");
//if this is disabled no character will appear
//use to filter out bad characters
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"text field should return");
return YES;
}

Getting UItextfield value not using IBoutlet

I know this is a basic question but i´m a little confused, so i hope you can help me. I have a tableview with multiple dynamic tableview cells, and inside each tableviewcell i have multiple textfields. Each cell has a different tag and also the textfields and i want to access the uitextfields values as you can imagine. My problem is, i´m not using IBoutlet for the textfields (it would be a enormous amount of IBoutlets)...I´m using - (void)textFieldDidEndEditing:(UITextField *)textField...but i just can´t seem to make the correct connections in the IB, this is my code:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102]) {
[textField resignFirstResponder];
}
After this, do i have to connect the respective UItexfield (and all textfields) to self? and then, do i have to use the editing did end event?...
Regards
I guess that the answer to this question is another question: What do you want to do with the text that the user enters?
I assume that you have some kind of data model that you want to store the data in.
If so, then when this function is called, you need to take the text that is already in the textField and save it to your data model immediately as it is entered.
For instance, you can access the text that was entered like this:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102])
{
[textField resignFirstResponder];
yourDataModel.stringToSave = textField.text;
}
}

Resources