简体   繁体   English

将一个ViewController中的managedObjectContext传递给Swift中的另一个ViewController?

[英]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. 我正在尝试从prepareForSeguecurrentVC设置我的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”按钮,但我无法弄清楚如何将MasterVCmanagedObjectContext传递给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上添加一个MasterVCprepareForSegue可以设置的属性。 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.

相关问题 将对象从一个ViewController传递到另一个ViewController很快就无效了 - Passing an object from one viewcontroller to another viewcontroller got nil in swift 快速将数组值从一个ViewController传递到另一个ViewController - Swift passing array values from one viewcontroller to another Swift:将数据从ViewController中的tableView传递到另一个ViewController - Swift: Passing data from a tableView in a ViewController to another ViewController 将数据从一个ViewController中的某些TextField传递到另一个ViewController中的TextView - Passing data from some TextFields in one ViewController to a TextView in another ViewController Swift 3将数据从一个ViewController传递到另一个ViewController - Swift 3 pass data from one ViewController to another ViewController 如何快速将图像从一个视图控制器传递到另一个视图控制器? - How to pass an image from one viewcontroller to another viewcontroller in swift? 使参数从一个ViewController传递到另一个的崩溃 - crash passing parameter from one viewcontroller to another 将UISlider值从一个ViewController传递到另一个 - Passing UISlider values from one ViewController to another 将对象从一个 ViewController 传递到另一个 - Passing the Object from one ViewController to another 将数据从一个ViewController传递到另一个ViewController - Passing data from one viewcontroller to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM