Storyboard & Segue - Passing datas am i doing it good?

廉价感情. 提交于 2019-12-10 11:17:10

问题


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

Three questions :

  • Is this the best way (more clean & beautiful) to do this ? & Why ?
  • When i do this : ProfileViewController *aProfileView = (ProfileViewController *)[segue destinationViewController]; is this instantiate a new view ? (Like it will create 2 Profile view).
  • do i need to clean (delete the "Profile View" somewhere ?) or it's doing it alone with the Navigation controller ?

// Code

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

回答1:


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:

  • yes, that's the best way. It's difficult to get prepareForSegue "beautiful" because of the generic class of destination view controller
  • no, you're not creating anything.
  • no, you don't have anything to clean up.


来源:https://stackoverflow.com/questions/10679559/storyboard-segue-passing-datas-am-i-doing-it-good

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!