1

ナビゲーションコントローラに3つのビューコントローラが組み込まれています。コアデータオブジェクトは保存されますが、関連するオブジェクトテーブルにポップアップしてから先に進むまでテーブルに表示されません。

storyboard

最初のビューコントローラは、テーブルを移入するfetchedResultsControllerを使用するテストのリストです。

2番目のビューコントローラは詳細ビューコントローラで、新しいテストを追加したり、詳細を編集したり、新しいテストを保存したり、テストに質問を追加することができます。また、質問のテーブルも含まれています。質問テーブルにはfetchedResultsControllerもあります。 Add Question button triggers the save

TestDetailsVC view

私が持っている問題は、私が保存されている既存のテストを持っているとき、私は質問に追加して行くとき、私はTestDetailsVCに戻っポップとき、そして、彼らは正しくただし、テーブルを読み込むことです新しいテストを追加して新しい質問を追加すると、コアデータにテストが保存され、QuestionDetailsVCの質問にコアデータが保存されます。しかし、TestDetailsVCにポップバックすると、新しい質問はテーブルに入力されません。しかし、TestListVCに戻り、次に進むと、質問によってテーブルが正しく読み込まれます。私は最初にテーブルにポップアップするときにテーブルにデータを入れる質問を得ようとしています。ビューが読み込まれたときにこのデータを再フェッチしないと、これらのオブジェクトがコアデータに保存されているため、元に戻ったときにテーブルは正常に動作しませんか?以前に保存したテストオブジェクトで正しく動作するが、新しく保存されたテストオブジェクトでは動作しないのはなぜですか?

TestDetailsVCに新しいテストを保存するための私のコードは次のとおりです。

//if we tap on a row, then we select that question to edit 
//if a question is selected, we will pass that information over to 
//the question editor view so it can be edited  
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    //make sure there is at least one question 
    //objs = means object selected 
    if let objs = controller.fetchedObjects, objs.count > 0 { 
     //if there is, keep track of the test which is selected 
     let question = objs[indexPath.row] 
     //pass along that test to the editor to be edited 
     //the sender is the selected test at that particular row 
     performSegue(withIdentifier: "EditQuestionSegue", sender: question) 
    } 
} 


//we need to get ready to do the segue before we call it 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let identifier = segue.identifier { 
     switch identifier { 
     case "EditQuestionSegue": 
      //find the question needing to be edited 
      //pass this along to the question editing view controller 
      if let destination = segue.destination as? QuestionDetailsVC { 
      if let question = sender as? Question { 
       destination.questionToEdit = question 
       //pass along the test that the question is 
       //related to also 
       if let test = testToEdit { 
        destination.testToEdit = test 
        } 
       } 
      } 
     case "AddNewQuestionSegue": 
      //if no test to edit got passed in, it means 
      //we're now editing a new test 
      //once we type in the test title, then we have to 
      //make sure the test has been saved if it's new test 
      //before we go on to edit questions 
      if testToEdit == nil { 

       saveTestBeforeAddingQuestion() 
       //make sure we set up as if new 
       //before we start adding questions 
       //these are called in the view did load 
      } 
      if let destination = segue.destination as? QuestionDetailsVC { 
       if let test = testToEdit { 
        destination.testToEdit = test 
       } 
      } 
     default: 
      print("no segue this time") 
     } 
    } 
} 

func saveTestBeforeAddingQuestion() { 

    var test: Test! 


    //if there's not a passed in value into the testToEdit core data 
    //object test entity, then we're going to edit as if new 
    if testToEdit == nil { 
     //then instantiate a new test object ready to be written to 
     test = Test(context: context) 
    } else { 
     test = testToEdit 
    } 

    if let title = titleTextField.text { 
     test.title = title 
    } 


    if let abrevTitle = abrevTitleTextField.text { 
     test.abrevTitle = abrevTitle 
    } 
    if let author = authorTextField { 
     test.author = author.text 
    } 
    if let publisher = publisherTextField { 
     test.publisher = publisher.text 
    } 


    ad.saveContext() 
    //since we have now saved a new test 
    //let's put that test into our testToEdit variable 
    //so we can pass it along to the next view controller 
    //during our segue 
    if let newTestCreated = test { 
     testToEdit = newTestCreated 
    } 
} 

新しい質問を保存するための私のコードは次のとおりです。

@IBAction func savePressed(_ sender: UIButton) { 

      var question: Question! 

     //if there's not a passed in value into the questionToEdit core data 
     //object test entity, then we're going to edit as if new 
     if questionToEdit == nil { 
      //then instantiate a new test object ready to be written to 
      question = Question(context: context) 
     } else { 
      question = questionToEdit 
     } 

     //make sure to relate the question added to the testToEdit test 
     question.test = self.testToEdit 

     //if there is something in the sentence text field 
     //then assign that value to the question.sentence attribute 
     //of the sentence entity in our core data context 
     if let sentence = questionSentenceTextField.text { 
      question.sentence = sentence 
     } 
     if let identifier = questionIdentifierTextField.text { 
      question.identifier = identifier 
     } 
     if let displayOrder = questionDisplayOrderTextField.text { 
      question.displayOrder = Int(displayOrder) as NSNumber? 
     } 

     // save the context 
     ad.saveContext() 

     _ = navigationController?.popViewController(animated: true) 

    } 

答えて

0

私はロードするために、質問表を取得しようとしている日を過ごしましたしかし、最終的には、ビューコントローラにポップアップしたときに、自動的にfetchedResultsControllerでテーブルをリロードしないため、バックアップがロードされていないことがわかりました。だから私はviewWillAppearメソッドでデータを再フェッチし、その後のtableViewを再ロードするために電話をかける必要があった:

(TestDetailsVC)

override func viewWillAppear(_ animated: Bool) { 
     guard testToEdit != nil else {return} 
     attempFetch() 
     tableView.reloadData() 
    } 
関連する問題