繁体   English   中英

关闭模态视图(第二视图)时,刷新ViewController中的核心数据-Swift

[英]Refresh Core Data in ViewController when Modal View (2nd view) is Dismissed - Swift

我试图弄清楚如何关闭模态视图后如何重新加载UIViewController。 我正在从View 1(我的UIVIewController)切换到Modal View,在那里我对Core Data进行了更新。 完成后,我保存核心数据并关闭模态视图,将用户送回视图1(UIViewController)。 问题在于UIViewController没有将更新的更改拉到Core Data(而是显示旧的信息,因为尚未刷新)。

我认为这是最接近的答案,但我在从Objective-C转换为Swift时遇到了麻烦。

有任何想法吗? 先谢谢您的帮助。

这是NSFetchedResultsController快速示例

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

            do {
                try fetchedResultsController.performFetch()
            } catch {
                print("An error occurred")
            }
    }

private lazy var fetchedResultsController: NSFetchedResultsController = {
        let animalsFetchRequest = NSFetchRequest(entityName: "Animal")
        let sortDescriptor = NSSortDescriptor(key: "classification.order", ascending: true)
        animalsFetchRequest.sortDescriptors = [sortDescriptor]

        let frc = NSFetchedResultsController(
            fetchRequest: animalsFetchRequest,
            managedObjectContext: self.context,
            sectionNameKeyPath: nil,
            cacheName: nil)

        frc.delegate = self

        return frc
}()

// delegate method

func controllerDidChangeContent(controller: NSFetchedResultsController) {
            // update UI
}

我对这个问题的建议是创建将通知View 1的委托。

例如:

在呈现的视图控制器中创建委托:

protocol NotifyReloadCoreData
    func notifyDelegate()
end

创建视图控制器的属性:

var delegate: NotifyReloadCoreData?

当按保存或类似的东西时:

delegate.notifyDelegate()

在您看来1

class UIViewController1: UIViewController, NotifyReloadCoreData

并执行功能

func notifyDelegate(){
    // reload core data here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM