iPhone - 跟踪后退按钮事件

发布于 2024-10-09 02:38:45 字数 2237 浏览 1 评论 0原文

我有一个 tableView,其中列出了我的文档目录的内容。我里面有一些 zip 文件。如果我触摸 tableView 中的文件,相应的 zip 文件将被解压缩并提取到临时目录中(在我的例子中是 newFilePath )。解压后的内容列在下一个 tableView 中。当我触摸后退按钮时,目录中的内容会再次列出。

例如,假设我的文档目录中有四个 zip 文件。

ongs.zip、videos.zip、files.zip、calculation.zip

当我运行应用程序时,所有四个文件都列在 tableView 中。当我触摸ongs.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];

谢谢您的回答。

I have a tableView which lists the contents of my document directory. I have some zip files in that. If I touch a file in the tableView, the corresponding zip file is unzipped and extracted in a temporary directory(newFilePath in my case). The contents unzipped is listed in the next tableView. When I touch the back button, the contents in the directory is listed again.

For example, consider that I have four zip files in my document directory.

songs.zip, videos.zip, files.zip, calculation.zip

When I run the application, all the four files are listed in the tableView. When I touch songs.zip, this file is extracted in the newFilePath and its contents are pushed to the next tableView. When I touch back, the previous tableView, i.e, the four files in the document directory are listed again. Everything works perfect.

The problem is, the extracted files in the newFilePath remains there itself. They occupy the memory unnecessarily. I want them to be removed from that path when I touch the back button, i.e, I want to make newFilePath empty when the back button is touched.

I tried for it. But, no use. I tried removeItemAtPath: method in viewWillAppear: and also in viewWillDisappear:. But it didnt work in both the cases.

Is there any other method to track the action of the back button? I want an event to take place when the back button is touched. So please help me by sharing your ideas. Here is my code for your verification.

This is my 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];

Thank you for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-10-16 02:38:45

要创建自己的操作,您需要自定义后退按钮。但我认为 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"];

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

for creating your own action you need custom back button.But i think viewWillDisAppear also can do solve your problem.

use this code for solving your problem

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];

This may help you.

Edit:

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

     NSString *databaseFile = [documentsDirectoryPath stringByAppendingPathComponent:@"newFilePath"];

use these lines in your code if you have directory inside the document directory.

无人问我粥可暖 2024-10-16 02:38:45

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

you can add the custom back button on Navigation Bar. Create the method, which will fire on click event of Back button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文