2011-10-27 65 views
0

我是新iPhone的發展,需要help.I txt文件正在開發包含一個UITextView.I一個iPhone應用程序具有的txt文件中。我想使用一個UITextView的文件清單顯示文本文件的內容,但我無法得到它在所有顯示任何內容。我使用這些代碼想要的UITextView顯示txt文件:顯示在一個UITextView

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
NSFileManager *manager = [NSFileManager defaultManager]; 
self.frlistName2 = [[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil]pathsMatchingExtensions:[NSArray arrayWithObject:@"txt"]]; 
NSString *obj = @".txt"; 
frlisturls2 = [[NSMutableArray alloc] initWithObjects:nil]; 
NSString *temp; 
NSMutableString *mp3Path = [[NSMutableArray alloc] init]; 
int j =0; 
for(int i =0 ; i< [frlistName2 count]; i++) 
{ 
    mp3Path = [NSMutableString stringWithString:documentsDirectoryPath]; 
    [mp3Path appendString:@"/"]; 

    temp = [frlistName2 objectAtIndex:i]; 
    if([temp hasSuffix :obj]) 
    { 
     [mp3Path appendString: temp]; 
     [frlisturls2 addObject: mp3Path]; 
     j++; 
    } 
    [mp3Path release]; 
} 
NSLog(@"frlisturls2 is%@:",frlisturls2); 
[super viewDidLoad]; 
fileName = [[NSString alloc]init]; 
fileName=[frlisturls2 objectAtIndex:iRow ]; 
NSLog(@"the fileName is %@",fileName); 
stringSources = [NSString stringWithContentsOfFile: fileName encoding:NSUTF8StringEncoding error:nil]; 
frtextview=[[UITextView alloc]init]; 
[frtextview setText:stringSources]; 
NSLog(@"the file is :%@",stringSources); 

文件名顯示正確的URL,但stringSource沒有words.Sorry爲長後,我希望您的幫助!

+0

一個很好的回答位於[這裏] [1] [1]:http://stackoverflow.com/questions/3865330/load-rtf-or-text-file-into-uite xtview-iphone-SDK – Kiarash

回答

1

這是我讀txt文件,以txtView

但有時該文件不能用文本編輯器打開,

NSString *filePath = @"Users/macbookpro/Library/Application 
Support/iPhone Simulator/4.3.2/Applications/ ... 
/Documents/sample.txt"; 
NSString *txtContent = [[NSString alloc] 
            initWithContentsOfFile:filePath 
            encoding:NSUTF8StringEncoding 
            error:&error]; 

負荷txtContent您textView.text