Je me request si quelqu'un peut expliquer la différence entre l'utilisation d'un segue se détendre et l'utilisation de la délégation dans l'exemple que j'ai ci-dessous:
J'ai un FriendsTableViewController
par un array
d'amis et un autre AddFriendTableViewController
avec une fonction pour append un friend
à FriendsTableViewController
.
Voici comment j'envoie datatables de mon AddFriendViewController
avec un unwind segue
AddFriendViewController
:
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Check whether the Done button was tapped. // If it wasn't, instead of saving the friend, the method returns without doing anything else. if (sender != self.doneButton) return; // See whether there's text in the text field. if (self.nameTextField.text.length > 0) { // If there's text, create a new friend and give it's properties the input from the text fields. self.friend = [[Friend alloc] initWithName:self.nameTextField.text dateOfBirth:self.birthdayDatePicker.date numberOfGifts:0]; } }
C'est ainsi que j'ajoute datatables de AddFriendTableViewController
au array
dans FriendsTableViewController
avec l' unwind segue action
:
#pragma mark - Actions - (IBAction)unwindSegue:(UIStoryboardSegue *)segue { // Resortingeve the source view controller, the controller that is unwinding from. AddFriendTableViewController *soruce = [segue sourceViewController]; // Resortingeve the soruce view controller's friend object. Friend *friend = soruce.friend; // See whether the item exists. // If it's nil, either the Cancel button closed the screen or the text field had no text, so you don't want to save the item. if (friend != nil) { // If it does exist, add the item to the friends array. [self.friends addObject:friend]; // Reload the data in the table. [self.tableView reloadData]; } }
Maintenant, cela fonctionne comme je le souhaite, donc j'espère ne pas enfreindre les règles de stackoverflow ou offenser qui que ce soit, mais je voulais juste savoir quelle est la différence entre mon code exemple et si le même scénario a été fait methods pour AddFriendViewController
. Si certains peuvent expliquer ce serait génial!
L'utilisation d'un segue de déroulement est très similaire à celle d'un délégué, avec les avantages suivants:
Les inconvénients sont
prepareForSegue
avec beaucoup de noms d'identifiants de twigment Le code que vous avez semble bon. Je restrais avec ça.