Difference between unwind segue and delegation

2019-05-07 17:53发布

I wonder if anybody can explain the difference between using a unwind segue and using delegation in the example I have below:

I have a FriendsTableViewController populated by an array of friends and another AddFriendTableViewController with a function to add a friend to the FriendsTableViewController.

This is how I send the data from my AddFriendViewController with a unwind segue:

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

This is how I add the data from the AddFriendTableViewController to the array in FriendsTableViewController with the unwind segue action:

#pragma mark - Actions

- (IBAction)unwindSegue:(UIStoryboardSegue *)segue
{
    // Retrieve the source view controller, the controller that is unwinding from.
    AddFriendTableViewController *soruce = [segue sourceViewController];

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

Now this works as I want so I hope I'm not breaking any stackoverflow rules or offending anyone, but I just wanted to know what is the difference between the way my example code is used and if the same scenario was made but with custom delegate methods for the AddFriendViewController. If some can explain it would be great!

1条回答
男人必须洒脱
2楼-- · 2019-05-07 18:01

Using an unwind segue is very similar to having a delegate, with the following advantages:

  • You don't need to implement any dismiss logic
  • You don't need to pass references up and down the navigation stack
  • You don't need to declare a delegate protocol
  • It's trivial to unwind back a number of stages in your app

The disadvantages are

  • Dependent on using storyboards (which may hamper reusability)
  • If there are a lot of them, it can lead to the same messiness as you can get in prepareForSegue with a lot of branching off identifier names
  • If you decide to present the view controller via some other method (not a segue) then you can't unwind back from it

The code you have looks good. I'd stick with it.

查看更多
登录 后发表回答