4

EDIT based on provided answer:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedName = quizNames[indexPath.row]
    performSegueWithIdentifier("saveSegue", sender: self)
}



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "saveSegue" {
            let inputController = segue.destinationViewController as? QAInputViewController
            inputController?.quizName = selectedName 

        }
    }

I have done some research on here trying to figure out exactly how to use prepareForSegue in order to pass the selected Core Data object from a UITableView cell to a second View Controller, but what i found and tried to implement did not work out for me as it seems some of the code might be outdated. Here is what i am trying to do:

  1. I populate a UITableView with an array of names of type Name which is my NSManagedObject. When i select a specific name it will segue me to an input screen where a question and answer will be submitted. I want to make sure that the question and answer that are submitted stay assigned to the previously selected name. I am not sure how to set this up in prepareForSegue. I did set up my data model with relationships, so if i did that correctly it should work once i understand how to implement it. Is there some way i can check the currently selected name so that i know any question and answer that is inputted will be saved to that name?

  2. Relative to my above question, i want the title of the second View Controller to show the name of the previously selected name.

Basically i am just looking for the guidance on to get this implemented in Swift 2. Thank you very much for your help.

EDIT: Passing Core Data objects from UITableViewCell to another View Controller

The above link is the solution i tried implementing that was not working for me. Specifically:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showTaskDetail" {
        let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
        let indexPath = self.tableView.indexPathForSelectedRow()
        let thisTask = fetchedResultsController.objectAtIndexPath(indexPath!) as! TaskModel
        detailVC.detailTaskModel = thisTask
        detailVC.delegate = self

    }
2
  • "what i found and tried". What exactly did you try? Can you show us? Commented Jan 25, 2016 at 2:02
  • will edit with the link from what i found and tried Commented Jan 25, 2016 at 2:10

1 Answer 1

2

This is how I do it:

At the top of my class I'll have a var that stores the selected item:

var selectedTask: Task?

Then in my tableViews didSelectRow method:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedTask = tasks[indexPath.row]
    performSegueWithIdentifier("showTaskDetail", sender: self)
}

And finally:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showTaskDetail" {
        let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
        detailVC.detailTaskModel = selectedTask
        detailVC.delegate = self
    }
}

However, I'd be more inclined to just pass some kind of ID/name and perform another fetch with a new moc rather than pass around core data objects.

You'll get less threading issues this way and it will make things far easier should you need to deep link into your app, from say, a notification or something.

Mocs are cheap, use them once, and throw them away.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the reply @beau. I am going to give this a shot. If i am already using an array to store my core data objects can i use that instead of creating another var to store the selected item? Also, do i need a var in my detailVC to store the selected item as well? I have seen that implemented in some answers here on SO
It entirely depends on how you will be using it. It helps if you have a var in your detailVC so that you have somewhere to pass your object to. If you already have an array, thats fine too, just store the selected indexPath instead (rather than "task") and use that to pull your object from the array so you can pass it to the detailVC.
thank you. What i did was put my array selectedName = quizNames[indexPath.row] does that seem correct? the last name line with assigning the delegate is not working for me though Also, i went to assign the core data object to the title so the name will show, but i am still getting nil when it is unwrapped. Not sure if i am doing something incorrectly
Are you assigning the entire object to the title? Or the title property of your core data object? Silly question probably. Anyway, if it's nil, then you probably haven't set it, and that might be worth asking a separate question.
...just an update. Seems i was able to get it to work messing around a bit. I got rid of the the didSelectRowAtIndexPath function and added in if let index = tableView.indexPathForSelectedRow?.row into my prepareForSegue function, plus selectedName = quizNames[index] and that seemed to get it to work as i intended. It was working before i did that, the problem was it was pushing my next VC twice which was strange. Seems to be all good now though. I appreciate the help. Without people like you for guidance i would have been stuck! Thank you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.