'Pushnavigation is not working with custom alert in swift

I am using my custom alert to log out of the application

in custom alert, I have tried to log out with firebase after the successful logout using navigation push controller to sign in the controller but it will not redirect to sign in screen

Here is my custom alert code

let customAlert = self.storyboard?.instantiateViewController(withIdentifier: "CustomAlertID") as! AlertViewController
        customAlert.titleLbl = "Log out"
        customAlert.imageTitle = "logout"
        customAlert.descryptionText = "You will be returned to the login screen. Are you sure you want to logout?"
        customAlert.okBtnText = "LOG OUT"

        DispatchQueue.main.async {
            customAlert.providesPresentationContextTransitionStyle = true
            customAlert.definesPresentationContext = true
            customAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
            customAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
            self.present(customAlert, animated: true, completion: nil)
        }

Here is my logout click button code

 let firebaseAuth = Auth.auth()
          do {
            try firebaseAuth.signOut()
              let controller = self.storyboard?.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
              self.navigationController?.pushViewController(controller, animated: true)

              DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                  self.navigationController?.popToRootViewController(animated: true)
              }

              let domain = Bundle.main.bundleIdentifier!
              UserDefaults.standard.removePersistentDomain(forName: domain)
              UserDefaults.standard.synchronize()
              print(Array(UserDefaults.standard.dictionaryRepresentation().keys).count)
          } catch let signOutError as NSError {
              self.dismiss(animated: true, completion: nil)
            print("Error signing out: %@", signOutError)
          }
        }

without the alert view, it will works



Solution 1:[1]

In order for the navigationController property to not be equal to nil, you must add your custom alertViewController to the navigation stack using pushViewController method. In your case, you are only presenting a custom alertViewController that does not know anything about your navigationController

There is another way to implement:

After initializing a custom view controller, you should pass it the necessary closure functions that should work after pressing the button

Example:

let customAlert = self.storyboard?.instantiateViewController(withIdentifier: "CustomAlertID") as! AlertViewController
        customAlert.titleLbl = "Log out"
        customAlert.imageTitle = "logout"
        customAlert.onClickExit = { [weak self] in
            self?.navigationController?.popViewController(animated: true)
        }
...

Solution 2:[2]

After Logout, you should set Another Root view controller and Your navigationController Stack Must be Clear.

let controller = self.storyboard?.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
let signInNavController = UINavigaionController(rootViewController: controller)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = signInNavController

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sherzod
Solution 2 Maziar Saadatfar