0

I'm developing an app to log book that children have read. So I would like to know how to make the page transition back to the menu page after clicking the "save" button. I also want to make the alert that shows "Data has been saved!". Below are my codes.

@IBOutlet weak var newBookSaveButton: UIButton!

    @IBAction func newBookTapped(_ sender: Any) {
        guard let uid = Auth.auth().currentUser?.uid,
                      let data = bookData() else {
                          return
                      }
                db.collection("new reading").document(uid).setData(data)
            }
            
        
            func bookData() -> [String: Any]? {
                guard let title = bookTitleTextField.text,
                      let author = bookAuthorTextField.text,
                      let summary = bookSummaryTextField.text else {
                          return nil
                      }
                let data: [String: Any] = [
                    "bookTitle": title,
                    "bookAuthor": author,
                    "bookSummary": summary
                ]
                return data
        
                self.transitionToMenu()
            }
    func transitionToMenu() {
       
        let MenuViewController = storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.MenuViewController) as? MenuViewController
        
        view.window?.rootViewController=MenuViewController
        view.window?.makeKeyAndVisible()
    }
}

With this code, I still unable to transition back to the Menu page. Your help are very much appreciated.

Guaringue
  • 3
  • 3
Abeeget
  • 21
  • 6
  • You should read up on using a segue to move between controllers as that will also allow you to easily navigate back to the parent viewController. There's a great answer here on SO [Passing data between view controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/9736559#9736559). It's a good idea to do a bit of research before posting a question as it, and an answer, may have already been asked. If that's not what you're asking, can you clarify the question? – Jay Dec 05 '21 at 15:06

1 Answers1

0

You can use this function:

func transitionToMenu() {
    let alert = UIAlertController(title: nil, message: "Data has been saved!", preferredStyle: .alert)
    alert.view.alpha = 0.5
    alert.view.layer.cornerRadius = 15
    self.present(alert, animated: true)
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
        alert.dismiss(animated: true)
        if let navController = self.navigationController {
            navController.popViewController(animated: true)
        } else {
            self.dismiss(animated: true, completion: {})
        }
    }
}