Search code examples
iosswiftios13

Swift - Run code when user dismiss UIReferenceLibraryViewController


I need to run a piece of code when user dissmisses the UIReferenceLibraryViewController. But my code that I previously coded isn't working on iOS 13.

Here is the code I wrote for iOS 12:

override func viewDidLoad() {
  if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: word) {
    let ref: UIReferenceLibraryViewController = 
      UIReferenceLibraryViewController(term: word)
      ref.reactive
      .trigger(for: #selector(onboardNav.viewDidDisappear(_:)))
      .observe { _ in self.handleModalDismissed() }

      self.present(ref, animated: false, completion: nil)
   }
}

func handleModalDismissed() { // I need to run this function when user presses "Back" button
    self.showAlert(error: false, word: "")
}

Solution

  • I just found an answer! You need to subclass a UIReferenceLibraryViewController, and override viewWillDisappear. Like so:

    class ReferenceLibraryViewControllerWithDismiss: UIReferenceLibraryViewController {
        override func viewWillDisappear(_ animated: Bool) {
            // your code
        }
    
    }
    

    And then present ReferenceLibraryViewControllerWithDismiss:

    let vc = ReferenceLibraryViewControllerWithDismiss()
    self.present(vc, animated: true)
    

    Happy coding!