A
A
Andrew2021-01-10 23:32:49
Swift
Andrew, 2021-01-10 23:32:49

How to update data in ViewController after dismiss() in popup?

Friends, good day. Quite new to Swift. I apologize in advance if this is a dumb question.

There is a method that receives a list of counterparties via API:

class SupplierViewController: UIViewController {

    @IBOutlet weak var suppliersTableView: UITableView!
    
    var suppliers: [Supplier] = []
    
    func fetchSupplierList()  {
        SupplierService.fetch(url: "http://localhost:8000/finance/api/suppliers/") { result in
            switch result {
            case .success(let suppliers):
                self.suppliers = suppliers
                self.suppliersTableView.reloadData()
            case .failure(let error):
                print("Failed to fetch suppliers", error)
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        suppliersTableView.delegate = self
        suppliersTableView.dataSource = self
        
        fetchSupplierList()
    }
    
    @IBAction func addButtonPressed(_ sender: UIButton) {
        performSegue(withIdentifier: "addSupplierSegue", sender: self)
    }
    
}


By clicking on the Add button, I go to the view with the form, after filling in the fields and performing POST, return to the table and reload the data from the server.

class AddSupplierViewController: UIViewController {

    @IBOutlet weak var name: UITextField!
    
    @IBOutlet weak var shortName: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    @IBAction func addSupplierPressed(_ sender: UIButton) {
        guard let name = name.text else { return }
        guard let shortName = shortName.text else { return }
        
        SupplierService.createSupplier(url: "http://localhost:8000/finance/api/suppliers/", name: name, short_name: shortName, inn: "123456") { error in
            if let error = error {
                print("Failed to create", error)
                return
            }
            DispatchQueue.main.async {
                self.dismiss(animated: true, completion: nil)
            }
            
        }
    }
}

Tried to shove the fetchSupplierList() call into the dismis' compliment, tried it in AddSupplierViewController's viewWillDissapear. Tried it in SupplierViewController's viewDidAppear. Nothing succeeded :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2021-01-11
@G_r_i_n_v_i_c_h

True to use delegates .
You can try hacking through parent and type casting. I do not recommend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question