ios - 在 Swift 中将 managedObjectContext 从一个 ViewController 传递到另一个 ViewController?

标签 ios swift core-data uiviewcontroller

我当前的项目是 Swift 中创建笔记的 Core Data 项目。我正在尝试从 prepareForSegue 中的 currentVC 设置我的 destinationVC 的 managedObjectContext,我正在尝试弄清楚如何在 Swift 中执行此操作.

我使用 Apple 的“固定代码”在 Swift 中设置了一个 CoreData 项目。 CoreData 堆栈在 AppDelegate.swift 中设置。

目前,我创建了一个方法,如果托管对象上下文中没有注释,则在 AppDelegate 中生成虚拟注释。 有效。

应用程序打开时的初始 View 是主从 VC 设置,显示我的托管对象上下文中的对象。 有效。我可以点击一个注释,它会显示在 DetailViewController 上。这主要是 Xcode 在我开始项目时创建的“固定”代码。

我在 MasterViewController 上创建了一个 UIBarButtonItem“添加”按钮,但我不知道如何传递 MasterVCmanagedObjectContextdestinationViewController。当我单击它时,它会按应有的方式转至 DetailViewController

这是我的 MasterVC 的属性:

var detailViewController: DetailViewController? = nil
var addNoteViewController:AddNoteViewController? = nil  // I added this
var managedObjectContext: NSManagedObjectContext? = nil

这是我在 MasterViewController 中的 prepareForSegue 方法。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }

    if segue.identifier == "addNote" {
        println("segue.identifier is addNote")
        // I think I need to add stuff here so the destination VC "sees" the managedObjectContext
    }
}

我想我还需要在 AddNoteVC 上添加一个 MasterVCprepareForSegue 可以设置的属性。那么我该如何在 Swift 中做到这一点呢?

最佳答案

要将属性添加到您的 AddNoteViewController,只需添加一个 var:

import CoreData

class AddNoteViewController: UIViewController {

    var managedObjectContext : NSManagedObjectContext?

然后你可以在你的 segue 中设置这个属性:

if segue.identifier == "addNote" {
    println("segue.identifier is addNote")
    // I think I need to add stuff here so the destination VC "sees" the managedObjectContext

    let controller = (segue.destinationViewController as! UINavigationController).topViewController as! AddNoteViewController
    controller.managedObjectContext = managedObjectContext
}

关于ios - 在 Swift 中将 managedObjectContext 从一个 ViewController 传递到另一个 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28614013/

相关文章:

ios - 如何确定用户是否已使用 Graph API 从 Facebook iOS 注销?

ios - 如何使用一对多关系将一个对象附加到 CoreData 中的另一个对象?

iphone - 如何在 Core Data 中保存实体并应用关系?

ios - 核心图形和渐变

ios - 使用 Rails 嵌套属性时如何将 _destroy 标志添加到 RestKit PUT 请求

objective-c - 在 PDF : Tm or Td/TD? 中查找文本

objective-c - 不止一次重命名 Core Data 中 ID 的实体? (第三版)

iphone - UIViewContentModeScaleAspectFit 不适用于 UIScrollView subview UIImageView

ios - 将我自己的 Swift 类导入 Playground

ios - 在 Swift 中实现核心数据验证方法