【问题标题】:How to reload UIViewController data from App Delegate如何从 App Delegate 重新加载 UIViewController 数据
【发布时间】:2015-11-05 08:59:03
【问题描述】:

我有一些从 appDelegate 调用的方法,用于将更改与持久存储和 iCloud 同步。

方法和 appDelegate 工作正常,我的应用同步更改正常; 然而当应用程序为mergingChangespersistentStoreDidChange 时,我正在尝试刷新view controller 数据并将view controller title 更改为同步。

我已尝试更改 UIViewController 标题文本,但在合并或 persistentStoreWillChange 时它不会更改,同样当对视图控制器集合使用 reloadData() 方法时,应用程序在展开可选值时以意外 nil 崩溃.

问题是该项目在UITabController 中有许多tableviewcolectionview,所以我真的需要在窗口中刷新view controller 的数据,而不仅仅是一个特定的视图。有人知道如何从appDelegate 刷新viewcontroller 数据吗?

func mergeChanges(notification: NSNotification) {
    NSLog("mergeChanges notif:\(notification)")
    if let moc = managedObjectContext {
        moc.performBlock {
            moc.mergeChangesFromContextDidSaveNotification(notification)
            self.postRefetchDatabaseNotification()
        }
    }
    let vc = CollectionViewController()
    let view = self.window?.rootViewController

    vc.title = "Syncing"
    view?.title = "Syncing"
}

func persistentStoreDidImportUbiquitousContentChanges(notification: NSNotification) {
    self.mergeChanges(notification);
}

func storesWillChange(notification: NSNotification) {
    NSLog("storesWillChange notif:\(notification)");
    if let moc = self.managedObjectContext {
        moc.performBlockAndWait {
            var error: NSError? = nil;
            if moc.hasChanges && !moc.save(&error) {
                NSLog("Save error: \(error)");
            } else {
                // drop any managed objects
            }

            moc.reset();
        }

       let vc = CollectionViewController()
       vc.title = "Syncing"

        // reset UI to be prepared for a totally different
        // don't load any new data yet.
    }
}

func storesDidChange(notification: NSNotification) {
    // here is when you can refresh your UI and
    // load new data from the new store
    let vc = CollectionViewController()
    // vc.collectionView.reloadData()

    NSLog("storesDidChange posting notif");
    self.postRefetchDatabaseNotification();
}

【问题讨论】:

    标签: ios swift uiviewcontroller icloud appdelegate


    【解决方案1】:

    对于上述功能,您可以使用 NSNotification 在您想要更新时向多个类触发该通知。

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 2012-09-05
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多