通过UINavigationController设置NSString对象,prepareForSegue

时间:2015-03-21 20:54:48

标签: ios objective-c

每次在我的应用中按下表格单元格时,我都会尝试设置动态NSString值。我可以使用下面的代码将变量传递给我的自定义UINavigationController -

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

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *page = [menuItems objectAtIndex:indexPath.row];
    [[segue destinationViewController] setGrabPage: page];

}

问题是grabPage仅在NavigationController中设置,而不是在导航控制器中显示的顶视图。如何将此值传递到顶视图?

1 个答案:

答案 0 :(得分:0)

如果你的destinationViewController是一个UINavigationController,你应该将它转换为UINavigationController并获取导航控制器的rootViewController。

将它投射到第一个控制器的类型,你很高兴

编辑:添加了示例代码

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

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *page = [menuItems objectAtIndex:indexPath.row];

    UINavigationController *nav = (UINavigationController *)[segue destinationViewController];
    MyViewController *vc = (MyViewController *)(nav.viewControllers[0]);
    [vc setGrabPage: page];
}