2

Merry Christmas! I am currently making a "reminders" app at the moment. My "secondary" TableViews fail to pass data back into the "main" TableView. (please see image below). I need the data to pass back into the "main" TableView so that I can save it and display that into another TableView, so the user can see the tasks they have put in.

My Storyboard, segue names in Red

The code inside the IconTableViewController (and ShowTableViewController):

var userImage: String = ""
var userTitle: String = ""
var userDesc: String = ""
var frequency: String = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let navigationViewController = segue.destination as? UINavigationController {

        guard let viewController = navigationViewController.topViewController as? NewItemTableViewController else { return }

        let userImageNew = userImage
        let userTitleOld = userTitle
        let userDescOld = userDesc
        let frequencyOld = frequency

        viewController.selectedImage = userImageNew
        viewController.userTitle = userTitleOld
        viewController.userDesc = userDescOld
        viewController.selectedFrequency = frequencyOld
    }
}

The code inside NewItemTableViewController:

var selectedImage: String = ""
var selectedFrequency: String = ""
var userTitle: String = ""
var userDesc: String = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showSelect" {
        let userImageOld = selectedImage
        let userTitleNew = titleTextField.text
        let userDescNew = descTextView.text
        let frequencyOld = selectedFrequency

        let viewController = segue.destination as! ShowTableViewController
        viewController.userImage = userImageOld
        viewController.userTitle = userTitleNew!
        viewController.userDesc = userDescNew!
        viewController.frequency = frequencyOld
    }

    if segue.identifier == "iconSelect" {
        let userImageOld = selectedImage
        let userTitleNew = titleTextField.text
        let userDescNew = descTextView.text
        let frequencyOld = selectedFrequency

        let viewController = segue.destination as! IconTableViewController
        viewController.userImage = userImageOld
        viewController.userTitle = userTitleNew!
        viewController.userDesc = userDescNew!
        viewController.frequency = frequencyOld
    }
}

Thank you very much for your help!

Kamran
  • 14,987
  • 4
  • 33
  • 51
Anthony
  • 133
  • 10

2 Answers2

1

Seems like NewItemTableViewControlleris not yet top viewController on the navigation stack. Try accessing it in the same trivial way,

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let viewController = segue.destination as? NewItemTableViewController {

        let userImageNew = userImage
        let userTitleOld = userTitle
        let userDescOld = userDesc
        let frequencyOld = frequency

        viewController.selectedImage = userImageNew
        viewController.userTitle = userTitleOld
        viewController.userDesc = userDescOld
        viewController.selectedFrequency = frequencyOld
    }
}

You should put a breakpoint inside above method prepare(for segue and see if it is being called. Because i feel you don't have unwind segue as these viewControllers are just pushed on the navigation stack. So in that case you can also use viewWillDisappear to set the data by accessing NewItemTableViewController from the navigation stack. Something as below,

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if let vc = self.navigationController?.viewControllers.first(where: { $0 is NewItemTableViewController }) as? NewItemTableViewController {
        // Set data.
    }
}
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • You're correct, the prepare (for segue...) method doesn't get called at all. However, using the viewWillDisappear method, it gives me the following error: Value of type 'UIViewController' has no member 'selectedImage' – Anthony Dec 25 '18 at 05:24
  • @Anthony Yes, i edited the answer. Please check, you need to cast it as `NewItemTableViewController` as above. – Kamran Dec 25 '18 at 05:27
0

You should use Delegate to push data back to NewItemTableViewController. You can refer this thread: Pass data back to previous viewcontrolle

Zorot
  • 21
  • 6