首页 文章

当模态视图(第二视图)被解除时,在ViewController中刷新核心数据 - Swift

提问于
浏览
1

我试图找出如何在解除模态视图后重新加载我的UIViewController . 发生了什么事情我从视图1(我的UIVIewController)到模态视图,我对核心数据进行了更新 . 完成后,我保存核心数据并关闭模态视图,将用户发送回View 1(UIViewController) . 问题是UIViewController没有将更新的更改提取到核心数据(而是提供旧信息,因为它尚未刷新) .

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

有任何想法吗?在此先感谢您的帮助 .

2 回答

  • 1

    这是快速的NSFetchedResultsController示例

    override public 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")
                }
        }
    
    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
    }()
    

    委托方法

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

    我对此问题的建议是创建将通知View 1的委托 .

    例如:

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

    protocol NotifyReloadCoreData
        func notifyDelegate()
    end
    

    创建视图控制器的属性:

    var delegate: NotifyReloadCoreData?
    

    当按保存或类似的东西:

    delegate.notifyDelegate()
    

    在你的视图中1

    class UIViewController1: UIViewController, NotifyReloadCoreData
    

    并实现功能

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

相关问题