0

How can I update the datasource array for a tableview when the back button is tapped on a second view?

I didnt see how to use seques/prepare/unwind on this case. I tried UINavigationControllerDelegate too and had no success.

Scenario:

First VC has a tableview with invoices information. When tap on a cell it shows a second VC with a particular invoice information.

Second VC has an action to make payment. When tap on make payment it shows a third VC (a modal).

After tapping make payment on the third VC, it return to the second VC (unwind). It works.

When Im back on the second VC, there is a back button (< Invoices) that I would like to go back to the first VC(invoices). Going back works. The problem is that I need to update the datasource when taping back on the navigation bar.

Save button works too. It updates the datasource (segues/unwind)

Story Board

VC1 members:

var invoices = [Invoice]()
var client: Client?
var invoice: Invoice?
var filteredInvoices = [Invoice]()

VC2 members:

var invoice: Invoice?
var client: Client?
var invoiceItems : [InvoiceItem] = [InvoiceItem]()

VC3 members:

var amountPaid: Decimal = 0.00
var totalInvoice: Decimal = 0.00
var dateTransaction: String? = nil
var invoice: Invoice?
Renan Aguiar
  • 245
  • 5
  • 22
  • Please review https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?s=1|137.5666 – rmaddy Mar 10 '18 at 17:01
  • ... or, if the invoice has been persisted somehow, refresh the model in `viewDidAppear`. – shallowThought Mar 10 '18 at 17:13
  • rmaddy, i see nothing about back button. just segues. Segues work on add, sabe... not on back on nav bar. – Renan Aguiar Mar 10 '18 at 17:20
  • shallowThought, i tried it, but the thingg is, i cant send the data back from 2 to 1. it goes nil all the time. It prints nav deleg. extension InvoiceViewController: UINavigationControllerDelegate { func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { print("nav deleg") (viewController as? InvoicesViewController)?.invoice = invoice } – Renan Aguiar Mar 10 '18 at 17:21
  • @RenanAguiar There are plenty of answers that don't use segues. The point is to give you ideas about using delegation to pass data back. – rmaddy Mar 10 '18 at 17:26
  • rmaddy, im not being rude but i couldnt see an answer for that case. When Save is pressed, it works. The problem is only on the back buttom! – Renan Aguiar Mar 10 '18 at 17:30

2 Answers2

0

Detect Back button pressed:

class SomeViewController : UIViewController {
    
    override func didMove(toParentViewController parent: UIViewController?) {
        
        super.didMove(toParentViewController: parent)
        
        //Back button pressed
        if parent == nil {
            
            //Update your data source here
        }
    }
}
Community
  • 1
  • 1
user1046037
  • 16,755
  • 12
  • 92
  • 138
  • it is not even called when i get back. – Renan Aguiar Mar 10 '18 at 17:35
  • is the destination view controller pushed ? like `navigationController?.pushViewController(someVC, animated: true)` – user1046037 Mar 10 '18 at 17:38
  • from invoices to invoice, VC1 to VC2 ==> let destination = segue.destination as? InvoiceViewController, let indexPath = tableView.indexPathForSelectedRow?.row AND from invoice to makepayment. VC2 to VC3==> let payButton = UIAlertAction(title: "Make Payment", style: .default, handler: { action in self.performSegue(withIdentifier: "makePayment", sender: self)}) – Renan Aguiar Mar 10 '18 at 17:41
0

I solved the issue by unwinding from VC3 (make payment) to VC1 (invoices).

Why? It makes more sense after entering a payment to go back to the invoice list and not the invoice detail.

I check if its getting back from detail(VC2) or makepayment(VC3) and make the appropriate calls for every scenario.

Renan Aguiar
  • 245
  • 5
  • 22