0

I have a class PopUpViewController that access class MessageController method handleLogout() with sharedInstance static let. But when the method is accessed and I try to present another view controller (loginController), I get this warning in console:

"error view is not in the window hierarchy!"

Can someone help?

class PopUpViewController: UIViewController {
    lazy var logOutButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("Logout", for: UIControlState())
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        button.addTarget(self, action: #selector(logOut), for:.touchUpInside)
        button.tintColor = .white
        return button
    }()

    func logOut(){
        removeAnimate()
        MessageController.sharedInstance.handleLogout()
    }
    ...
}

MessageController

class MessageController: UITableViewController{
    static let sharedInstance = MessageController()

    func handleLogout(){
        do {
            try FIRAuth.auth()?.signOut()
        }catch let logoutError{
            print(logoutError)
        }
        // to je za ime na vrhu ob prijavi nastavi se vrednost
        let loginController = LoginController()
        loginController.messagesController = self
        GIDSignIn.sharedInstance().signOut() // google logout

        self.present(loginController, animated: false, completion: nil)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Moonwalker4z
  • 85
  • 1
  • 1
  • 8
  • refer this link, http://stackoverflow.com/questions/27608338/adding-a-view-to-the-window-hierarchy. – KAR Oct 06 '16 at 13:26
  • 1
    where would you like to call this method? the earliest point is in the `-viewDidAppear(_:)`, because before the presenter view is not in the view hierarchy, that that is why you get the warning. – holex Oct 06 '16 at 13:35
  • It seems as if the `MessageController` instance you create as the `sharedInstance` variable doesn't get presented. – Phillip Mills Oct 06 '16 at 13:43
  • @holex i don't want to call it in viewDidAppear(_:) because then it will automatically log out. That is why I create an sharedInstance... Do you have any suggestions ? – Moonwalker4z Oct 06 '16 at 14:36
  • @PhillipMills What do you mean ? I call it in func logOut in PopUpViewController – Moonwalker4z Oct 06 '16 at 14:37
  • @Moonwalker4z, unfortunately you __cannot__ call earlier in the view lifecycle; you may need to redesign your workflow this or that way. – holex Oct 06 '16 at 14:38
  • @holex I even tried this, but it didn't work DispatchQueue.main.async { self.present(loginController, animated: false, completion: nil) } – Moonwalker4z Oct 06 '16 at 14:40
  • @Moonwalker4z, it is not a threading problem, you are already on the _main_ thread but at the _wrong_ time; you need to 'wait' for the view controller goes into to view hierarchy and then you can use it as a _presenter_ – that happens from `-viewDidAppear(_:)` and stays until the `-viewWillDisappear(_:)` invoked, therefore the earliest_ point_ when you can present your content is inside the `–viewDidAppear(_:)`. – holex Oct 06 '16 at 15:06
  • @holex Yes I understand but I don't know how to implement this in the whey that it would work similar as now. If i present loginController on view did appear it will always go to loginController but I want it to be triggered at button click. – Moonwalker4z Oct 06 '16 at 15:12
  • @Moonwalker4z I see that you call a method on your `MessageController`'s shared instance but I don't see anywhere that you display the shared instance. – Phillip Mills Oct 06 '16 at 15:36

0 Answers0