故事板和Segue – 传递数据我做得好吗?

我正在使用故事板和赛格。 我想从“联系人列表”(tableView)切换到“configuration文件视图”(ScrollView)。

三个问题

  • 这是最好的方式(更干净和美丽)做到这一点? 为什么?
  • 当我这样做:ProfileViewController * aProfileView =(ProfileViewController *)[segue destinationViewController]; 这是实例化一个新的观点? (就像它会创build2个configuration文件视图)。
  • 我需要清理(删除“configuration文件视图”的地方?),或者它是单独使用导航控制器?

//代码

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

你的第一个例子很好。 你没有创build任何东西,只是得到你的目标控制器的引用。 设置一个像这样的variables,可以在目标视图控制器上设置多个属性,而不必一遍又一遍地重复。

所以,要回答你的具体问题:

  • 是的,这是最好的方法。 因为目标视图控制器的通用类,所以很难获得prepareForSegue“美丽的”
  • 不,你没有创造任何东西。
  • 不,你没有东西要清理。