将CoreData密钥传递给新的ViewController

时间:2016-07-27 04:16:04

标签: ios swift core-data

我正在处理我的应用程序的一部分,该部分将用作用户的日记,在其中他们可以添加条目,然后查看或删除它们。我为CoreData提供了一些Swift教程,我已经确认CoreData设置正常。但是,我在将值传递给s = str.reverse #=> "592=egap&5=f?php.egap" a = s.partition(/\d+/) #=> ["", "592", "=egap&5=f?php.egap"] b = a.reverse #=> ["=egap&5=f?php.egap", "592", ""] b.map(&:reverse) #=> ["page.php?f=5&page=", "295", ""] 时遇到问题,UIViewController将在保存之后显示日志。在一种配置中,我试过它没有显示任何数据,在另一种配置中,无论选择什么条目,数据都保持不变,并且在下面的设置中,此行journalEntryViewer.journalViewerTextView.text = journalTextToPass返回"在解包可选值时找到nil。 " 以下是充当列表的UITableViewController的代码:

import UIKit
import CoreData

class JournalCollectionViewer: UITableViewController {
    var JournalEntry = [NSManagedObject]()
    var journalTextToPass:String!
    var journalTitleToPass:String!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.reloadData()
    }
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)
    self.tableView.reloadData()
    let appDelegate =
        UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "JournalEntry")
    do {
        let results =
            try managedContext.executeFetchRequest(fetchRequest)

        JournalEntry = results as! [NSManagedObject]
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }
}
// MARK: - Table view data source

@IBOutlet weak internal var journalCollectionTable: UITableView!


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return JournalEntry.count
}
override func tableView(tableView: UITableView,
               cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell =
        tableView.dequeueReusableCellWithIdentifier("JournalCell")

    let entry = JournalEntry[indexPath.row]

    cell!.textLabel!.text =
        entry.valueForKey("journalDate") as? String
    return cell!
}
    func selectedTableCell(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let entry = self.JournalEntry[indexPath.row]

    self.performSegueWithIdentifier("ShowEntryViewer", sender: entry)
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowEntryViewer" {
        let journalEntryViewer = segue.destinationViewController as! JournalEntryViewer
        journalEntryViewer.self.navigationItem.title = journalTitleToPass
        journalEntryViewer.journalViewerTextView.text = journalTextToPass
    }
}
}

在查看器的Swift文件(journalEntryViewer)中,我所做的就是声明类和UITextView。不用说,我不知所措。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

当您为IBOutlet方法中的prepareForSegue对象分配值时,它没有初始化,因此您需要传递字符串对象并将该字符串对象分配给journalViewerTextViewviewDidLoad的{​​{1}}。

首先在JournalEntryViewer中声明两个实例var,就像这样

JournalEntryViewer

现在在var journalTitle: String? var journalText: String?

中使用此var
viewDidload

现在在override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = journalTitle self.journalViewerTextView.text = journalTitle }

journalTitle方法中传递此journalTextprepareForSegue
JournalCollectionViewer

新编辑:

您的代码问题是您没有使用委托方法override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "ShowEntryViewer" { let journalEntryViewer = segue.destinationViewController as! JournalEntryViewer journalEntryViewer.journalTitle = journalTitleToPass journalEntryViewer.journalText = journalTextToPass } } 而不是您正在使用其他错误的didSelectRowAtIndexPath尝试实现UITableView的委托方法

selectedTableCell

现在您正在传递入口对象,因此请更改func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let entry = self.JournalEntry[indexPath.row] self.performSegueWithIdentifier("ShowEntryViewer", sender: entry) } 这样的

prepareForSegue