【发布时间】:2011-05-30 08:24:21
【问题描述】:
我有一个 tableView 列出了我的文档目录的内容。我有一些 zip 文件。如果我在 tableView 中触摸一个文件,相应的 zip 文件将被解压缩并提取到一个临时目录中(在我的情况下为newFilePath)。解压后的内容列在下一个 tableView 中。当我触摸后退按钮时,目录中的内容再次列出。
例如,假设我的文档目录中有四个 zip 文件。
songs.zip、videos.zip、files.zip、calculation.zip
当我运行应用程序时,所有四个文件都列在 tableView 中。当我触摸songs.zip 时,这个文件被提取到newFilePath 中,并且它的内容被推送到下一个tableView。当我触摸回来的时候,之前的tableView,即文档目录下的四个文件又被列出来了。一切都很完美。
问题是,newFilePath 中提取的文件本身仍然存在。它们不必要地占用内存。我希望在我触摸后退按钮时将它们从该路径中删除,即我想在触摸后退按钮时将 newFilePath 设为空。
我试过了。但是,没有用。我在viewWillAppear: 和viewWillDisappear: 中尝试了removeItemAtPath: 方法。但它在这两种情况下都不起作用。
还有其他方法可以跟踪后退按钮的操作吗?我希望在触摸后退按钮时发生一个事件。因此,请通过分享您的想法来帮助我。这是我的验证码。
这是我的didSelectRowAtIndexPath:
NSString *filePath = //filePath
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSLog(@"File exists at path: %@", filePath);
} else {
NSLog(@"File does not exists at path: %@", filePath);
}
ZipArchive *zip = [[ZipArchive alloc] init];
NSString *newFilePath = //newFilePath
[[NSFileManager defaultManager] createDirectoryAtPath:newFilePath withIntermediateDirectories:NO attributes:nil error:nil];
BOOL result = NO;
if([zip UnzipOpenFile:filePath]) {
//zip file is there
if ([zip UnzipFileTo:newFilePath overWrite:YES]) {
//unzipped successfully
NSLog(@"Archive unzip Success");
result= YES;
} else {
NSLog(@"Failure To Extract Archive, maybe password?");
}
} else {
NSLog(@"Failure To Open Archive");
}
iDataTravellerAppDelegate *AppDelegate = (iDataTravellerAppDelegate *)[[UIApplication sharedApplication] delegate];
//Prepare to tableview.
MyFilesList *myFilesList = [[MyFilesList alloc] initWithNibName:@"MyFilesList" bundle:[NSBundle mainBundle]];
//Increment the Current View
myFilesList.CurrentLevel += 1;
viewPushed = YES;
//Push the new table view on the stack
myFilesList.directoryContent = [AppDelegate getTemporaryDirectoryItemList:newFilePath];
[myFilesList setTitle:detailedViewController.strName];
[self.navigationController pushViewController:myFilesList animated:YES];
[myFilesList release];
感谢您的回答。
【问题讨论】:
-
removeItemAtPath:失败了,还是没有调用viewWillDisappear:? -
其实我只在 MyFilesList.m 中使用上面的代码。因此,如果我在 viewWillDisappear: 中使用 removeItemAtPath: ,它会在文件被提取后立即清除 newFilePath。它调用 removeItemAtPath: 并且它不起作用,因为我在 MyFilesList 中调用函数 MyFilesList。
标签: iphone uitableview ios4 nsfilemanager