【问题标题】:iOS: Should I use a delegate or an NSNotification in this situation?iOS:在这种情况下我应该使用委托还是 NSNotification?
【发布时间】:2017-12-01 17:41:13
【问题描述】:

我有一个不断更新新数据的数据库。我将此数据用作我的UITableView 的数据源。目前,我正在使用NSNotifications 提醒我的UITableView 插入、删除或更新新数据。但是,我一直认为使用delegates 会更好,因为它是一对一的。

这里有一些代码可以更好地演示发生了什么。

- (void)insertObject:(NSNotification *)notification {
    NSNumber *object = [notification object];

    [self.tableView beginUpdates];
    [self.data insertObject:object atIndex:0];
    [self.tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:0] withAnimation:NSTableViewAnimationEffectNone];
    [self.tableView endUpdates];
 }

【问题讨论】:

标签: ios objective-c delegates nsnotificationcenter


【解决方案1】:

在更大的范围内,数据库更新通常是您项目的许多部分可能感兴趣的事件。这就是 Core Data 本身使用通知而不是委托方法的原因。是的,你可以选择任何一种方式,但我倾向于在这种模式上效仿 Apple。 (顺便说一句,maddy 对您帖子的评论确实有很好的解释)

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2018-04-02
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多