情节提要& Segue-传递数据我做得很好吗? [英] Storyboard & Segue - Passing datas am i doing it good?

查看:62
本文介绍了情节提要& Segue-传递数据我做得很好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用情节提要&塞格斯.我想从联系人列表"(tableView)切换到个人资料视图"(ScrollView).

I'm using Storyboards & segues. I want to switch from "Contacts List" (tableView) to a "Profile view" (ScrollView).

三个问题:

  • 这是最好的方式(更干净更漂亮)吗? &为什么?
  • 当我这样做时:ProfileViewController * aProfileView =(ProfileViewController *)[segue destinationViewController];这是实例化的新观点吗? (就像它将创建2个个人资料"视图一样.)
  • 我是否需要清洁(删除配置文件视图"某处?),或者它是单独使用Navigation控制器完成的?

//代码

- (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;
    }
}

//其他执行此操作的方法:

// Other way to do this :

- (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];
    }
}

推荐答案

您的第一个示例很好.您无需创建任何内容,只需获取对目标控制器的引用即可.设置类似的变量,使您可以在目标视图控制器上设置多个属性,而不必一遍又一遍地进行转换.

Your first example is fine. You aren't creating anything, just getting a reference to your destination controller. Setting up a variable like that allows you to set multiple properties on the destination view controller without having to cast over and over again.

因此,要回答您的特定问题:

So, to answer your specific questions:

  • 是的,那是最好的方法.由于目标视图控制器的通用类,很难使prepareForSegue变得美丽"
  • 不,您没有创建任何东西.
  • 不,您没有任何要清理的东西.

这篇关于情节提要& Segue-传递数据我做得很好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆