[英]Passing an managedObjectContext from one ViewController to another ViewController in Swift?
My current project is a Core Data project in Swift that creates notes. 我当前的项目是Swift中的一个核心数据项目,用于创建笔记。 I'm trying to set the managedObjectContext of my
destinationVC
from the currentVC
in prepareForSegue
and I am trying to figure out how to do that in Swift. 我正在尝试从
prepareForSegue
的currentVC
设置我的destinationVC
的managedObjectContext,我试图弄清楚如何在Swift中做到这一点。
I've used Apple's "canned code" for setting up a CoreData project in Swift. 我使用Apple的“固定代码”在Swift中设置CoreData项目。 The CoreData stack is set up in
AppDelegate.swift
. CoreData堆栈在
AppDelegate.swift
设置。
At present, I created a method to generate dummy notes in the AppDelegate if there are no notes in the managed object context. 目前,如果托管对象上下文中没有注释,我创建了一种在AppDelegate中生成虚拟注释的方法。 That works.
这样可行。
The initial view when the app opens is a Master-Detail VC set up that displays the objects in my managed object context. 应用程序打开时的初始视图是Master-Detail VC设置,用于显示托管对象上下文中的对象。 That works.
这样可行。 I can click on a note and it displays on the DetailViewController.
我可以单击一个注释,它将显示在DetailViewController上。 This is mostly "canned" code Xcode creates when I started the project.
这主要是Xcode在我启动项目时创建的“预制”代码。
I created a UIBarButtonItem
"Add" button on the MasterViewController
, but I can't figure out how to pass the MasterVC
's managedObjectContext
to the destinationViewController
. 我在
MasterViewController
上创建了一个UIBarButtonItem
“Add”按钮,但我无法弄清楚如何将MasterVC
的managedObjectContext
传递给destinationViewController
。 When I click that it segues as it should to the DetailViewController
. 当我点击它它应该细分到
DetailViewController
。
Here are the properties from my MasterVC
: 以下是我的
MasterVC
的属性:
var detailViewController: DetailViewController? = nil
var addNoteViewController:AddNoteViewController? = nil // I added this
var managedObjectContext: NSManagedObjectContext? = nil
This is my prepareForSegue
method in the MasterViewController
. 这是我在
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
}
}
I think I also need to add a property on the AddNoteVC
that the MasterVC
's prepareForSegue
can set. 我想我还需要在
AddNoteVC
上添加一个MasterVC
的prepareForSegue
可以设置的属性。 So how do I do that in Swift? 那么我如何在Swift中做到这一点?
To add a property to your AddNoteViewController
, just add a var
: 要向
AddNoteViewController
添加属性,只需添加一个var
:
import CoreData
class AddNoteViewController: UIViewController {
var managedObjectContext : NSManagedObjectContext?
Then you can set this property in your segue: 然后你可以在你的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
}
Mystery solved: 谜团已揭开:
I added this line of code to the AddNoteViewController
, nothing in prepareForSegue
in MasterViewController.swift
我将这行代码添加到
AddNoteViewController
,在MasterViewController.swift
中的prepareForSegue
中没有任何内容
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.