我有視圖控制器A(FileListViewController)和B(TextFileViewController)。 A是一個UITableViewController。我現在正在做的是在控制器A中選擇一行後,我加載一個文本文件並在控制器B的UITextView中顯示該文本。設置UITextView的文本屬性後,屏幕不會相應地更新
以下是頭和實現部分(某些代碼被刪節)我的兩個控制器。
FileListViewcontroller接口:
@interface FileListViewController : UITableViewController {
NSMutableArray * fileList;
DBRestClient* restClient;
TextFileViewController *tfvc;
}
@property (nonatomic, retain) NSMutableArray * fileList;
@property (nonatomic, retain) TextFileViewController *tfvc;
@end
FileListViewController實現:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DBMetadata *metaData = [fileList objectAtIndex:indexPath.row];
if(!metaData.isDirectory){
if([Utils isTextFile:metaData.path]){
if(!tfvc){
tfvc = [[TextFileViewController alloc] init];
}
[self restClient].delegate = self;
[[self restClient] loadFile:metaData.path intoPath:filePath];
[self.navigationController pushViewController:tfvc animated:YES];
}
}
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath {
NSError *err = nil;
NSString *fileContent = [NSString stringWithContentsOfFile:destPath encoding:NSUTF8StringEncoding error:&err];
if(fileContent) {
[tfvc updateText:fileContent];
} else {
NSLog(@"Error reading %@: %@", destPath, err);
}
}
這裏是TextFileViewController接口:
@interface TextFileViewController : UIViewController {
UITextView * textFileView;
}
@property (nonatomic, retain) IBOutlet UITextView * textFileView;
-(void) updateText:(NSString *) newString;
@end
TextFileViewController實現:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)] autorelease];
textFileView = [[UITextView alloc] init];
}
- (void) updateText:(NSString *)newString {
NSLog(@"new string has value? %@", newString);
[textFileView setText:[NSString stringWithString:newString]];
NSLog(@"print upddated text of textview: %@", textFileView.text);
[[self textFileView] setNeedsDisplay];
}
(void)restClient:loadedFile:將在loadFile:intoPath:在disSelectRowAtIndexPath方法中完成後調用。
在TextFileViewController的updateText方法,從NSLog我看到文本屬性更新正確。但屏幕不會相應更新。我試過setNeedsDisplay但徒勞無功。我錯過了什麼?
在此先感謝。