My app has a UITableView that displays brief data about an item, and I'm trying to send the user to another viewController when they press on a cell. That viewController is named detailViewController and I want it to display more details about the selected cell. I'm using core data, and my problem lies here. I do not know how I would pass one specific object stored in cordite to another view controller. Here is my code:
var tasks: Array<AnyObject> = [] //Creating an array that will store fetched data
override func viewWillAppear(animated: Bool) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contexts: NSManagedObjectContext = appDel.managedObjectContext!
let fetchreq = NSFetchRequest(entityName: "toDo")
tasks = contexts.executeFetchRequest(fetchreq, error: nil)!
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CellID: NSString = "myCell"
var cell: CustomCell = tableView.dequeueReusableCellWithIdentifier(CellID) as CustomCell
if let indexpth = indexPath as NSIndexPath! {
var data: NSManagedObject = tasks[indexpth.row] as NSManagedObject
cell.theTextLabel?.text = data.valueForKeyPath("toDoName") as? String
cell.theDetailTextLabel?.text = data.valueForKeyPath("toDoNumber") as? String
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if segue.identifier == "showDetail" {
var detailVC: DetailViewController = segue.destinationViewController as DetailViewController
var index: NSIndexPath = self.tableView.indexPathForSelectedRow()!
var row: NSInteger = index.row
var myData: NSManagedObject = tasks[row] as NSManagedObject
var tName: AnyObject? = testData.valueForKeyPath("tName")
detailVC.detailTasks = myData
detailVC.detailName = tName
}
}
I'm not sure how I would pass an object stored in core data to another view controller, I've tried many things but it doesn't seem to work. Thanks guys!
I solved my problem after trying a few things, the code below worked for me. In my DetailViewController I changed the from a label to a text field to display the information. I created a variable detailName of type string and dropped my data in there.Thanks for you help guys!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
var detailVC: DetailViewController = segue.destinationViewController as DetailViewController
var selectedItem: NSManagedObject = tasks[self.tableView.indexPathForSelectedRow()!.row] as NSManagedObject
detailVC.detailName = selectedItem.valueForKey("tName") as String
}
Check to make sure that your detailVC
has been allocated and initialized before you pass along any data. I'm guessing this is the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.