1

I'm getting this error because of I'm presenting alert in another VC, but how to solve this.

My code is :

{
     let message = res[0]["message"] as! String
     //If session expaired move to login page
     if message == "Session Expired" {
         //Session Expired
         DispatchQueue.main.async {
             //Remove all navigations
             self.navigationController?.viewControllers.removeAll()
             //Check whether the user logined or not
             UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
             //Clear user defaults
             SharedClass.sharedInstance.clearDataFromUserDefaults()

             let lvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LVC") as! LoginViewController
             let appDelegate = UIApplication.shared.delegate as! AppDelegate
             appDelegate.window?.rootViewController = lvc
         }
     }
     //Call alert function
     self.showAlert(title: "", msg: message)
 }

Alert function :

//Alert function
extension UIViewController {
    func showAlert(title: String, msg: String) {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

How to present alert properly in my case....

manishsharma93
  • 1,039
  • 1
  • 11
  • 26
Naresh
  • 16,698
  • 6
  • 112
  • 113

2 Answers2

1

Try presenting alertController on rootViewController as,

extension UIViewController {
    func showAlert(title: String, msg: String) {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController?.present(alert, animated: true, completion: nil)
        }
    }
}

The viewController you are calling showAlert method on is not currently presented viewController in the view hierarchy so either you should get the latest presented viewController in the view hierarchy or try presenting on the rootViewController.

You can check this to get the currently presented viewController.

Naresh
  • 16,698
  • 6
  • 112
  • 113
Kamran
  • 14,987
  • 4
  • 33
  • 51
1

Just use

self.navigationController?.popToRootViewController(animated: false)

or

 self.navigationController?.setViewControllers([LoginViewController], animated: true)

instead of

self.navigationController?.viewControllers.removeAll()

m1sh0
  • 2,236
  • 1
  • 16
  • 21
  • actually here loginVC is not my root VC , dashboard VC in root VC. So I want to make Login VC as root vc. Then if I write this code`self.navigationController?.popToRootViewController(animated: false)` it will work. Am I right.... – Naresh Jul 25 '19 at 06:01
  • yes, or you can do something like self.navigationController?.viewControllers = [LoginViewController] – m1sh0 Jul 25 '19 at 06:03
  • What is the meaning of this self.navigationController?.viewControllers = [LogoinViewControllre] – Naresh Jul 25 '19 at 06:08
  • Error: `Cannot assign value of type '[LoginViewController].Type' to type '[UIViewController]'` – Naresh Jul 25 '19 at 06:23
  • sorry, the idea is to change the root view controller, I edit my answer with the right code – m1sh0 Jul 25 '19 at 06:42
  • Again it's getting `Cannot convert value of type '[LoginViewController].Type' to expected argument type '[UIViewController]'` – Naresh Jul 25 '19 at 06:44
  • LoginViewControoler should be an instance of your view controller that should be presented. – m1sh0 Jul 25 '19 at 06:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196959/discussion-between-ios-and-m1sh0). – Naresh Jul 25 '19 at 06:47