【问题标题】:How to let user create a file with path-Objective c [closed]如何让用户使用路径目标 c 创建文件 [关闭]
【发布时间】:2012-12-09 19:47:52
【问题描述】:

我需要能够得到用户定义的路径:

NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:Usersetpath  contents:data attributes:nil];

我需要定义用户集路径

【问题讨论】:

  • 我担心 SO 不使用你的代码,所以你必须问你的用户他们想要的路径是什么。

标签: objective-c file path save


【解决方案1】:

这是一个选择路径的例子:

__block NSOpenPanel* panel= [NSOpenPanel openPanel]; // __block because it's used
                                                     // inside the block
__block NSString* path;
[panel setCanChooseDirectories: YES];
[panel setCanChooseFiles: NO];
[panel beginSheetModalForWindow: self.window completionHandler:^(NSInteger result)
 {
     if(result==NSOKButton)
     {
         path= [[panel URL] absoluteString]; // This is the path the user choosed
     }
     // Check also for the case that the user did hit the cancel button
     panel=nil; // The panel retains the block and the block retains the panel
                // so break the strong ref cycle by setting panel to nil.
 }];

编辑

我忘了说你应该将文件名附加到路径中。

【讨论】:

    【解决方案2】:

    什么样的“来自用户的路径”? 您可以使用文本字段让用户输入字符串并使用它来提示他输入路径 - 但不清楚您的问题到底是什么。

    另外,请注意,在典型的 iOS 应用程序中,用户通常不会遇到路径结构。实际上,您应该对用户隐藏路径,并在幕后将文件存储在适当的目录中。

    编辑:刚刚看到您没有指定 iOS 或 Mac OS,所以我上面的评论当然适用于 iOS...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 2013-05-06
      • 2013-12-23
      相关资源
      最近更新 更多