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

Ios 在Swift中将managedObjectContext从一个ViewController传递到另一个ViewController?,ios,swift,core-data,uiviewcontroller,Ios,Swift,Core Data,Uiviewcontroller,我当前的项目是Swift中创建注释的核心数据项目。我正在尝试从prepareForSegue中的currentVC设置我的destinationVC的managedObjectContext,我正在尝试找出如何在Swift中实现这一点 我用苹果的“固定代码”在Swift中建立了一个CoreData项目。CoreData堆栈在AppDelegate.swift中设置 目前,我创建了一个方法,用于在托管对象上下文中没有注释时在AppDelegate中生成虚拟注释有效。 应用程序打开时的初始视图是一个

我当前的项目是Swift中创建注释的核心数据项目。我正在尝试从
prepareForSegue
中的
currentVC
设置我的
destinationVC
的managedObjectContext,我正在尝试找出如何在Swift中实现这一点

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

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

应用程序打开时的初始视图是一个主详细信息VC设置,显示我的托管对象上下文中的对象有效。我可以单击注释,它会显示在DetailViewController上。这大部分是我启动项目时Xcode创建的“固定”代码

我在
MasterViewController
上创建了一个
UIBarButtonItem
“添加”按钮,但我不知道如何将
MasterVC
managedObjectContext
传递到
destinationViewController
。当我单击该按钮时,它将按应有顺序进入
DetailViewController

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
    }
}
以下是my
MasterVC
的属性:

var detailViewController: DetailViewController? = nil
var addNoteViewController:AddNoteViewController? = nil  // I added this
var managedObjectContext: NSManagedObjectContext? = nil
这是我的
MasterViewController
中的
prepareforsgue
方法

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
上添加
MasterVC
prepareforsgue
可以设置的属性。那么如何在Swift中实现这一点呢?

要将属性添加到
AddNoteViewController
,只需添加
var

import CoreData

class AddNoteViewController: UIViewController {

    var managedObjectContext : NSManagedObjectContext?
然后可以在序列中设置此属性:

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
}
谜团解开:

我将这行代码添加到
AddNoteViewController
,在
MasterViewController.swift中的
prepareForSegue
中没有任何内容


让managedObjectContext=(UIApplication.sharedApplication().delegate as!AppDelegate)。managedObjectContext

请避免使用此类标题并保持简洁。谢谢,亚伦!我刚想出来,我来这里发布答案。我把它添加到了
AddNoteViewController.swift
中,它成功了<代码>让managedObjectContext=(UIApplication.sharedApplication().delegate as!AppDelegate)。managedObjectContext
我将尝试您的解决方案,看看它有什么作用。我尝试了您的解决方案,结果它崩溃了。日志显示
无法将“MyApp.AddNoteViewController”(0x109cac5f0)类型的值强制转换为“UINavigationController”(0x10b8d2820)。
@AdrianB您可能正在使用其他segue类型。如果是这种情况,segue的目标视图控制器将不同。您可以使用
let controller=segue.destinationViewController作为!改为添加NoteViewController
。谢谢。我致力于GitHub并最终炸毁了故事板。我认为Github做了一个改变,其中包含了Xcode不喜欢的冲突#Whackamolet也可以,不过您可能想看看这篇文章:。在您的解决方案中,您正在从视图控制器中请求托管对象上下文。“更干净”的模式是告诉视图控制器要使用哪个上下文。(不过,在这种特殊情况下,这可能并不重要。)一旦我解决了我的GitHub问题和故事板问题,我将进行修补。非常感谢。