【问题标题】:Storyboard & Segue - Passing datas am i doing it good?Storyboard & Segue - 传递数据我做得好吗?
【发布时间】:2012-05-27 14:49:19
【问题描述】:

我正在使用 Storyboards 和 segues。我想从“联系人列表”(tableView)切换到“个人资料视图”(ScrollView)。

三个问题

  • 这是最好的方法(更干净、更漂亮)吗? & 为什么?
  • 当我这样做时: ProfileViewController *aProfileView = (ProfileViewController *)[segue destinationViewController];这是实例化一个新视图吗? (就像它会创建 2 个配置文件视图)。
  • 我需要清理(删除某处的“配置文件视图”吗?)还是单独使用导航控制器来清理?

// 代码

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showProfileSelected"]) 
    {
        // Get the Selected raw
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Profile *selectedProfile = [self.profilesTable objectAtIndex:indexPath.row];

        // Using segue --> Send the current selected profile to "ProfileView"
        ProfileViewController *aProfileView = (ProfileViewController *)[segue destinationViewController];
        aProfileView.currentProfile = selectedProfile;
    }
}

// 其他方式:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showProfileSelected"]) 
    {
        // Get the Selected raw
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Profile *selectedProfile = [self.profilesTable objectAtIndex:indexPath.row];

        // Using segue --> Send the current selected profile to "ProfileView"
        [segue.destinationViewController setCurrentProfile:selectedProfile];
    }
}

【问题讨论】:

    标签: iphone ios uitableview storyboard segue


    【解决方案1】:

    你的第一个例子很好。您没有创建任何东西,只是获取对目标控制器的引用。设置这样的变量允许您在目标视图控制器上设置多个属性,而无需一遍又一遍地进行转换。

    所以,回答您的具体问题:

    • 是的,这是最好的方法。由于目标视图控制器的泛型类,很难让 prepareForSegue “漂亮”
    • 不,你没有创造任何东西。
    • 不,你没有任何东西要清理。

    【讨论】:

    • 感谢您的回答。这是必要的吗? (ProfileViewController *) 因为没有它它也可以工作。
    • 您可能会收到编译器警告,但是?您在那里所做的称为强制转换,它让编译器知道它正在处理什么类(例如 ProfileViewController,而不是 UIViewController)
    • 不,我没有收到任何编译器警告 - 我不确定那是演员表。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多