Search code examples
swiftuitableviewcore-datansfetchedresultscontroller

Finding nil in prepareForSegue when trying to set UITextField text from Core data object


I have a UITableView that uses an NSFetchedResultsController to fetch my data for the UITableView. When a user selects a row in the UITableView, I have an edit modal view slide up with two UITextFields for the user to edit the data that was in the UITableViewCell. What I am trying to accomplish is setting the text of each UITextField with the text that was in the UITableViewCell from Core data. I am doing this so that the user does not have to re-type their data just to make an edit.

The issue is when I try to set the text for each UITextField in prepareForSegue, I am getting a fatal error " unexpectedly found nil while unwrapping an Optional value". I know that the data is there, so I am unsure exactly why it is finding nil. I followed this stackoverflow post to write my prepareForSegue function correctly when using an NSFetchedResultsController. This is my code:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       if segue.identifier == "edit" {
        if let inputController = segue.destinationViewController as? QAInputViewController {
            let uri = currentDeck!.objectID.URIRepresentation()
            let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
            inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
            if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
                let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                inputController.questionInput.text = data?.question
                inputController.answerInput.text = data?.answer
            }

        }

    }

}

Thank you for your help.


Solution

  • I found that my the crash was happening because my destination view controller's view was not loaded yet when trying to assign text to each UITextField.

    So I added two variables of type String in my destination view controller to hold the data from my prepareForSegue function instead of trying to assign the text from the selected UITableView row directly to the UITextField and then in viewWillAppear I assigned those variables to each UITextField.

      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          if segue.identifier == "edit" {
            if let inputController = segue.destinationViewController as? QAInputViewController {
                let uri = currentDeck!.objectID.URIRepresentation()
                let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
                inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
            if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
                let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
                    inputController.selectedQuestion = data!.question
                    inputController.selectedAnswer = data!.answer
             }
          }
       }
    }
    

    In my destination view controller:

    var selectedQuestion: String = ""
    var selectedAnswer: String = ""
    
    override func viewWillAppear(animated: Bool) {
    
        questionInput.text = selectedQuestion
        answerInput.text = selectedAnswer
    }