【问题标题】:iPhone - tracking back buttoniPhone - 追踪后退按钮
【发布时间】: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


【解决方案1】:

哦,那很简单:

在加载视图中,

    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] 
                                            initWithTitle:@"Back" 
                                                    style:UIBarButtonItemStylePlain 
                                                   target:self 
                                                   action:@selector(backButtonHit)];

-(void)backButtonHit
{
// removeItemAtPath: newFilepath stuff here
  [self.navigationController popViewControllerAnimated:YES];
}

【讨论】:

  • 嗨。谢谢你。我应该把这个放在哪里? self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonHit)];
  • 你也可以放入ViewDidLoad。
  • 哦,是的,我从不使用 InterfaceBuilder,所以它对我来说是 loadview,对你们所有人来说都是 viewDidLoad。您可以放置​​一个断点并检查是否正在调用 backButtonHit 方法。如果是,并且如果它导航回来,那么 removeItemAtPath 逻辑是错误的......
猜你喜欢
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多