问题
I have a Master-Detail VC set up and I've figured out how to set up a segue to my Detail VC. The code below doesn't crash, but it also doesn't send a value for the label to the VC I'm pushing to.
I'm stumped as to how I pass the value of detailViewController.detailItem = selectedNote?.noteBody
to my DetailViewController
.
The code below compiles. It does not crash. But it also doesn't set anything on my DetailViewController
.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// This is my object
var selectedNote: Note?
/*
I do NOT want to use prepareForSegue. I have 2 diff't sets of notes I'm dealing with. One in
the managedObjectContext and the other is in filteredObjects array.
*/
// This gets selectedNote from the fetchedResultsController
if tableView == self.tableView {
selectedNote = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Note // <--this is "everything"
} else {
// This get a note from the filteredObjects array
// This statement is necessary to unwrap the optional
if let filteredObjects = filteredObjects {
selectedNote = filteredObjects[indexPath.row]
}
}
// Set up the detail view controller to show.
let detailViewController = DetailViewController()
// This is nil. I want to to set a value on the detailVC before I segue
self.detailViewController!.detailItem = selectedNote?.noteBody
// Note: Should not be necessary but current iOS 8.0 bug requires it.
tableView.deselectRowAtIndexPath(tableView.indexPathForSelectedRow()!, animated: false)
// this does the segue.
navigationController?.pushViewController(detailViewController, animated: true)
}
I'm missing something very easy here, but I can't figure it out. Here is my DetailViewController
:
import UIKit
import CoreData
class DetailViewController: UIViewController {
@IBOutlet var detailDescriptionLabel: UILabel!
var note: Note?
var detailItem: AnyObject? = nil {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem as? Note {
note = detail
if let label = self.detailDescriptionLabel {
label.text = detail.noteBody
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
Here's my Note
class:
class Note: NSManagedObject {
@NSManaged var dateCreated: NSDate
@NSManaged var dateEdited: NSDate
@NSManaged var noteTitle: String
@NSManaged var noteBody: String
}
来源:https://stackoverflow.com/questions/28801266/segue-not-setting-value-to-detail-view-controller