i am calling a GET(method) API on viewDidAppear function of a view controller. i am presenting a new view controller using navigation controller over my first view controller. on the second view controller i am calling an API of Post Method to add another entry into my previous screen Get method API. But when I dismiss the second View Controller the Get API data remains the same and when i again runs the code the data was updated on the first view controller. Can someone tell me that how to check on first view controller that my second view controller is dismissed so that i can call API there.
Asked
Active
Viewed 870 times
1
-
2Could you please add code to your question. It a bit hard to understand. – dasdom Dec 20 '21 at 07:51
-
Try `viewWillDisappear()` – Kudos Dec 20 '21 at 07:52
-
Call the api in your viewWillAppear method of your first view controller – Muhammad Nawaz Dec 20 '21 at 08:40
-
@MuhammadNawaz it didn't work in viewWillAppear – Taimoor Arif Dec 21 '21 at 07:27
1 Answers
1
I got the solution for this. It didn't work by calling the API on viewDidAppear() or viewWIllAppear() . This will be done by using swift closures.
Below is the code:
class 1stViewController: UIViewController {
@IBAction func buttonTapped(_ sender: UIButton) {
guard let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else { return }
secondController.callbackClosure = { [weak self] in
print("call API")
}
self.navigationController?.pushViewController(secondController, animated: true)
}
}
On Second view Controller:
class SecondViewController: UIViewController {
var callbackClosure: ((Void) -> Void)?
override func viewWillDisappear(_ animated: Bool) {
callbackClosure?()
}
}

Taimoor Arif
- 750
- 3
- 19