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