我有一個名爲「MYFOLDER」的文件夾內有一個pdf文件test.pdf。如何在iphone中瀏覽文件?
我有一個名爲browse和textfield的按鈕。只要我點擊瀏覽按鈕,我需要瀏覽文件夾並選擇test.pdf文件(如GMAIL附件)。選擇特定文件後,我需要在文本字段中設置文件路徑。
如何做到這一點?
在此先感謝。
我有一個名爲「MYFOLDER」的文件夾內有一個pdf文件test.pdf。如何在iphone中瀏覽文件?
我有一個名爲browse和textfield的按鈕。只要我點擊瀏覽按鈕,我需要瀏覽文件夾並選擇test.pdf文件(如GMAIL附件)。選擇特定文件後,我需要在文本字段中設置文件路徑。
如何做到這一點?
在此先感謝。
爲什麼不是code?
我讚賞NSFileManager。
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:@"/MYFOLDER"];
NSArray *pdfList = [list filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF ENDSWITH '.pdf'"]];
NSLog(@"%@", pdfList);
也許這將幫助你
[[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf";];
如果文件保存在文檔目錄中會發生什麼? – 2012-04-02 06:42:28
Returns a directory enumerator object that can be used to perform a deep enumeration of the directory at the specified path.
- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path Parameters
path
The path of the directory to enumerate.
Return Value
An NSDirectoryEnumerator object that enumerates the contents of the directory at path.
If path is a filename, the method returns an enumerator object that enumerates no files—the first call to nextObject will return nil.
然後,你需要列出文件夾中的文件和文件夾,並繼續瀏覽。
NSError *error;
NSString *homeDirectory = NSHomeDirectory();
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:homeDirectory error:&error];
for(int i = 0; i < [dirContents count]; i++){
//Here you can browse dirContents
NSString *fileName = [dirContents objectAtIndex:i];
}
獲取錯誤,如[NSFileManager contentsOfDirectoryAtPath:]:無法識別的選擇器發送到實例0x4e05400' – sachi 2012-04-02 07:11:05
那裏。嘗試查看有關文件處理的Apple文檔。 – 2012-04-03 06:47:50
如果我知道文件夾我可以像你說的那樣使用。如果我不知道如何導航多個文件夾。 – sachi 2012-04-02 06:36:12
我收到錯誤:'NSPredicatepredicateWithFormat'未聲明(首次在此函數中使用)。我不知道它爲什麼會來。 @bitmapdata – sachi 2012-04-02 07:03:17
哎呀,對不起。我忘記了間距[NSPredicate predicateWithFormat:我已被更正的代碼.. – 2012-04-02 07:16:39