Search code examples
iosipadfileuitableviewreload

NSFilemanager doesn't save file when textFieldDidEndEditing is called a second time


I am having trouble with updating my tableView.

In the tableView of the popover managed by the rootviewcontroller, some items in my documents directory are displayed. In the detailViewcontroller, i change the names of those files by using the NSFilemanager. Regardless what I do, the tableView won't display the new ones. I get to see them if i close the app and open it again. At the moment, i try using notifications, but it doesn't work …

EDIT

I logged my documents directory, its definitely not a tableView problem, moreover, it works out well, but the second time I enter some text, nothing happens…

DetailViewController

- (void)textFieldDidEndEditing:(UITextField *)tf
{
    textLabel.text = textField.text;
    NSString* newName = textLabel.text;
    newName = [newName stringByAppendingPathExtension:@"txt"];
    NSString* newPath = [[currentFilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName]; 
    NSLog(@"%@",newPath);
    [[NSFileManager defaultManager] moveItemAtPath:currentFilePath toPath:newPath error:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];

}

RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
    self.clearsSelectionOnViewWillAppear = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSaved:) name:@"DataSaved" object:nil];
}

- (void)dataSaved:(NSNotification *)notification
{
    [self loadDirectoryContents];
    [self.tableView reloadData];
}

Solution

  • It works out when I type in something. But if i type in something the second time without having anything selected in the tableView, the fileManager doesn't move the item to the path. Any suggestions? It doesnt seem to be a tableView related Problem.

    I also thought I needed to set:

    currentFilePath = newPath;
    

    But that gave me a EXC_BAD_ACCESS.