我试图在一个segue之后更新一个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'处设置文本 编辑2:添加'didSelectRowAtIndexPath'
注意:segue是从MasterViewController指向DetailViewController(而不是来自 tableViewCell)
感谢您的帮助:)
答案 0 :(得分:1)
可能无法在prepareForSegue点加载文本视图。我刚刚开始使用故事板,并且发现我必须在目标视图控制器上设置字符串属性,然后在viewDidLoad或viewWillAppear中更新UI组件。