2011-11-27 75 views
1

我試圖更新一個UITextView的文本屬性後,在一個表視圖的行輕觸後發生。UITextView在更新後不更新文本

這裏是我的代碼

MasterViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

if ([[segue identifier] isEqualToString:@"4 Choices"]) { 
    NSLog(@"prepareForSegue is called"); 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
    NSString *questionContent = [[selectedObject valueForKey:@"questionContent"]description]; 
    self.questionContent = questionContent; 
    [segue.destinationViewController setTextViewWith:self.questionContent]; 
} 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSLog(@"didSelectRowAtIndexPath is called"); 
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
NSNumber *questionID = [selectedObject valueForKey:@"qID"]; 
int i = [questionID intValue]; 
if (i == 1) { 
    [self performSegueWithIdentifier:@"4 Choices" sender:self]; 
} 
else 
{ 
    [self performSegueWithIdentifier:@"5 Choices" sender:self]; 
} 
} 

DetailViewController.m

-(void)setTextViewWith:(NSString *)aText 
{ 
self.questionText = aText; 
NSLog(@"setTextViewWith is called"); 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
[self.questionView setText:self.questionText]; 
NSLog(@"viewWillAppear is called"); 
} 

編輯1:修改代碼,設置爲 'viewWillAppear中' EDIT 2文本: add'didSelectRowAtIndexPath'

注意:segue是從MasterViewController指出DetailViewController(而不是從 tableViewCell)

感謝您的幫助:)

回答

1

文本視圖可能不會在prepareForSegue點被加載。我剛剛開始講故事板,發現我必須在目標視圖控制器上設置字符串屬性,然後更新viewDidLoad或viewWillAppear中的UI組件。

+0

我試着在'viewWillAppear'中設置文本,但它不起作用。 – PTCHR

+0

在我建議的目標視圖控制器上創建新的字符串屬性後?您在prepareForSegue中設置字符串,然後在viewWillAppear中將textfield文本設置爲字符串。 – jrturton

+0

我已經在destinationViewController中設置了字符串屬性。我將在一分鐘內編輯這篇文章。 – PTCHR