Avatar ·

How to intercept the "Back" button press in UINavigationController and execute custom code?

Hello! I'm a beginner in iOS development with Swift, and I have a problem. In my app, there is a UINavigationController with several screens. I need to perform a specific action (e.g., save data or show an alert) when the user presses the standard "Back" button in the navigation bar. I tried overriding the `viewWillDisappear` method, but it gets called not only when pressing "Back", but also during any transition to another screen (e.g., when pushing a new controller). This is not suitable. Here is an example of my code: ```swift class SecondViewController: UIViewController { override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // This code triggers both when pressing "Back" and when navigating forward p ```