简体   繁体   English

使用情节提要板无法正确显示Subview-title

[英]Subview-title not displayed properly using storyboard

I have a tableview and a detailview, connected with segues. 我有一个表视图和一个详细视图,与segues连接。 I want the detailview's title to be the title of the selected cell. 我希望detailview的标题是所选单元格的标题。

If I use this code in GradeDetail.m , it works, but is static, of course: 如果我在GradeDetail.m使用此代码,则可以使用,但是当然是静态的:

self.navigationController.title = @"Title goes here";

If I use this on the other hand, it doesn't work, but should be dynamic: 另一方面,如果我使用它,它将不起作用,但是应该是动态的:

self.navigationItem.title = grade.fag;

I have the class Grade imported where necessary and "fag" (means "school subject" in Danish, not gay) property defined in the class. 我在必要时导入了班级Grade ,并在班级中定义了“ fag” (在丹麦语中是“学校主题”,不是同性恋)

Here is an excerpt of the code from the tableview: 这是tableview中的代码摘录:

...
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) 
{
    grades = [[NSMutableArray alloc] init];

    Grade* grade = [[Grade alloc] init];
    grade.fag = @"Dansk";
    grade.mundtlig = @"7";
    [grades addObject:grade];

...    

}
return self;
}

And the code pushing the detail-view: 以及推动详细视图的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{    
 GradeDetail *detailViewController = [[GradeDetail alloc] init];

Grade* grade = [self.grades objectAtIndex:indexPath.row];
detailViewController.grade = grade;

//[self.navigationController pushViewController:detailViewController animated:YES];
}

Notice from the above code, what I commented out: 从上面的代码中注意到,我注释掉了:

[self.navigationController pushViewController:detailViewController animated:YES];

If I remove the segue in the storyboard, and use this line of code instead, the detail-view shows the correct title, but the view isn't loading correctly. 如果我删除情节提要中的segue,并改用这行代码,则详细视图将显示正确的标题,但视图加载不正确。 (Read: I don't want to use this line of code, I want it to work in storyboard.) (阅读:我不想使用这一行代码,我希望它在情节提要中起作用。)

Please tell me if you want to see more of the code or have some of the above clarified. 请告诉我您是否想查看更多代码或澄清以上内容。

You'll need to override -prepareForSegue:sender: to get a hook into the incoming view controller and set any properties required. 您需要重写-prepareForSegue:sender:以钩住传入的视图控制器并设置所需的任何属性。 You'll also need to specify segue identifiers in the storyboard. 您还需要在情节提要中指定segue标识符。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowGradeDetails"])
    {
        GradeDetail *detailViewController = (GradeDetail *)segue.destinationViewController;
        NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
        Grade *selectedGrade = [self.grades objectAtIndex:selectedIndexPath.row];
        detailViewController.grade = selectedGrade;
    }
}

也许尝试设置viewController的标题: self.title = grade.fag;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM