1

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];
         }
     }
}
Community
  • 1
  • 1
Brosef
  • 2,945
  • 6
  • 34
  • 69

3 Answers3

0

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
    }
    ...
}
Eric Qian
  • 2,246
  • 1
  • 18
  • 15
0

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
     }
}
klein
  • 54
  • 6
0

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
Kai Schaller
  • 443
  • 3
  • 9