OGeek|极客世界-中国程序员成长平台

标题: ios - 在 UILabel 中显示核心数据计数,不断更新 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:56
标题: ios - 在 UILabel 中显示核心数据计数,不断更新

给定一个名为 Book 的核心数据实体,计算总共有多少 Book 并显示总数的最佳方法是什么在 UILabel 中? UILabel 需要随着书本对象的创建/删除(可能由应用程序中的其他对象创建/删除)进行更新。

实际上数书并不难。我的问题是,随着这个计数的变化,我们如何获得更新?



Best Answer-推荐答案


您可以监听与核心数据更改相关的通知 (NSManagedObjectContextObjectsDidChangeNotification) 并从中更新您的 UI。

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(handleDataModelChange name:NSManagedObjectContextObjectsDidChangeNotification object:myManagedObjectContext];

- (void)handleDataModelChangeNSNotification *)notification;
{
  NSSet *updatedObjects  = notification.userInfo[NSUpdatedObjectsKey];
  NSSet *deletedObjects  = notification.userInfo[NSDeletedObjectsKey];
  NSSet *insertedObjects = notification.userInfo[NSInsertedObjectsKey];  

  // update your UI with the new count
}

注意:不要忘记删除自己,

- (void)dealloc;
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

关于ios - 在 UILabel 中显示核心数据计数,不断更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21089812/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4