加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 展开segue和委托之间的区别

发布时间:2020-12-14 17:42:42 所属栏目:百科 来源:网络整理
导读:我想知道是否有人可以解释在下面的示例中使用展开segue和使用委托之间的区别: 我有一个由朋友数组填充的FriendsTableViewController和另一个AddFriendTableViewController,其中包含一个将朋友添加到FriendsTableViewController的功能. 这是我通过展开segue
我想知道是否有人可以解释在下面的示例中使用展开segue和使用委托之间的区别:

我有一个由朋友数组填充的FriendsTableViewController和另一个AddFriendTableViewController,其中包含一个将朋友添加到FriendsTableViewController的功能.

这是我通过展开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];
    }
}

这是我通过unwind segue操作将AddFriendTableViewController中的数据添加到FriendsTableViewController中的数组的方法:

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

现在这可以按照我的意愿工作,所以我希望我不会破坏任何stackoverflow规则或冒犯任何人,但我只是想知道我的示例代码的使用方式与是否使用自定义委托制作相同的方案之间的区别AddFriendViewController的方法.如果有人可以解释它会很棒!

解决方法

使用unwind segue与拥有委托非常相似,具有以下优点:

>您不需要实现任何解雇逻辑
>您无需在导航堆栈中上下传递引用
>您不需要声明委托协议
>在应用程序中解开许多阶段是微不足道的

缺点是

>取决于使用故事板(可能妨碍可重用性)
>如果它们中有很多,它可能导致同样的混乱,因为你可以在prepareForSegue中获得大量分支标识符名称
>如果您决定通过其他方法(而不是segue)呈现视图控制器,那么您无法从中退出

你的代码看起来不错.我坚持下去.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读