【问题标题】:iPhone - track back button eventiPhone - 回溯按钮事件
【发布时间】:2010-12-27 06:12:10
【问题描述】:

我有一个 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];

感谢您的回答。

【问题讨论】:

    标签: iphone objective-c uitableview ios4 uinavigationcontroller


    【解决方案1】:

    要创建自己的操作,您需要自定义后退按钮。但我认为 viewWillDisAppear 也可以解决您的问题。

    使用此代码解决您的问题

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    
    
    
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
     NSString *databaseFile = [documentsDirectoryPath stringByAppendingPathComponent:@"newFilePath"];
    
     NSFileManager *fileManager = [NSFileManager defaultManager];
     [fileManager removeItemAtPath:databaseFile error:NULL];
    

    这可能会对你有所帮助。

    编辑:

        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
        NSString *secondaryDirectoryPath = [secondaryDirectoryPath stringByAppendingPathComponent:@"secondary"];
    
         NSString *databaseFile = [documentsDirectoryPath stringByAppendingPathComponent:@"newFilePath"];
    

    如果文档目录中有目录,请在代码中使用这些行。

    【讨论】:

    • 谢谢。但是,这是删除文档目录中的所有文件。
    • 这怎么可能。首先确保您的路径是否正确。您在文档目录中还有多个文件。
    • 您是在文档目录中创建目录还是在文档目录中添加文件。
    • 如果它对你有帮助,那么它需要一个赞成票,如果它缩短了你的问题,那么就需要接受 ans。
    • 实际上,当我触摸 zip 文件时,它会在文档目录本身中创建一个临时目录并在那里提取。因此,当我在 viewWillDisAppear: 中使用 removeItemAtPath: 并在 didSelectRowAtIndexPath: 中再次调用 RootViewController 时,它会在下一秒创建并删除临时目录。这是我的问题。
    【解决方案2】:

    您可以在导航栏上添加自定义后退按钮。创建方法,该方法将在返回按钮的单击事件时触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多