2011-06-16 53 views
0

我有視圖控制器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但徒勞無功。我錯過了什麼?

在此先感謝。

回答

1

In - [TextFileViewController viewDidLoad]你正在創建一個UITextView,但其框架從未設置,並且它不會被添加到視圖層次結構中。

嘗試修改此:

textFileView = [[UITextView alloc] init]; 

這樣:

textFileView = [[UITextView alloc] initWithFrame:[[self view] bounds]]; 
[[self view] addSubview:textFileView]; 
0

問題是在TextFileViewControllerviewDidLoad方法中創建textFileView。當您撥打updateText(這是在TextFileViewController被推送之前發生)時,此方法尚未被調用。

您可以在致電[[self restClient] loadFile:metaData.path intoPath:filePath];之前強制加載視圖來解決此問題。

相關問題