简体   繁体   English

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

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

I'm trying to figure out how to reload my UIViewController after I dismiss a Modal View. 我试图弄清楚如何关闭模态视图后如何重新加载UIViewController。 What's happening is I segue from View 1 (my UIVIewController) to a Modal View where I make an update to Core Data. 我正在从View 1(我的UIVIewController)切换到Modal View,在那里我对Core Data进行了更新。 Upon completion, I save the Core Data and dismiss the Modal View, sending the user back to View 1 (the UIViewController). 完成后,我保存核心数据并关闭模态视图,将用户送回视图1(UIViewController)。 Problem is the UIViewController is not pulling the updated change to the Core Data (but instead is presenting the old information, because it has not been refreshed). 问题在于UIViewController没有将更新的更改拉到Core Data(而是显示旧的信息,因为尚未刷新)。

This was the closest answer I think that could work, but I'm having trouble translating from Objective-C to Swift. 我认为这是最接近的答案,但我在从Objective-C转换为Swift时遇到了麻烦。

Any ideas? 有任何想法吗? Thanks in advance for the help. 先谢谢您的帮助。

Here is quick NSFetchedResultsController example 这是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
}

My suggestion for this issue is to create delegate that will notify View 1. 我对这个问题的建议是创建将通知View 1的委托。

For instance: 例如:

in presented view controller create delegate: 在呈现的视图控制器中创建委托:

protocol NotifyReloadCoreData
    func notifyDelegate()
end

create property of view controller: 创建视图控制器的属性:

var delegate: NotifyReloadCoreData?

when press save or something like that : 当按保存或类似的东西时:

delegate.notifyDelegate()

in your View 1 在您看来1

class UIViewController1: UIViewController, NotifyReloadCoreData

and implement function 并执行功能

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

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

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