0
votes

After presenting a Tab bar controller, I can't dismiss tab bar controller. I also can't even tap my button after I reinstall without delete the app. Need to uninstall and reinstall the app then I am able to tap the button

I already tried some other way of dismiss the tab bar controller but still unable to dismiss the controller.

This is my current way to present controller after login

let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let loginVC = storyboard.instantiateViewController(withIdentifier: "Home")
self.present(loginVC, animated: true, completion: nil)

This is my current way to dismiss controller

@IBAction func btnLogout_TouchUpInside(_ sender: Any) {
    dismiss(animated: true, completion: nil)
  }

This is my root view

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        if defaults.bool(forKey: "isLoggedIn") {
            // Show Home screen
            window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "Home")

        } else {
            // Show Login screen
            window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "Login")
        }
        window?.makeKeyAndVisible()
3
There's nothing obviously wrong with your code. However it's also not completely clear either. We don't need to see the cookie and keychain stuff as it's not relevant. We do need to know where and when you are running the code to load the login. And the logout is on which screen? I've seen similar issues before from Code which uses multiple UIWindow instances and miss-manages them so I'd also ask if you are doing anything like that, or perhaps switching root view controllers.drekka
Is it could be my rootview controller? I did do some checking in AppDelegate. If there is user immediately show tab bar controller else show login view controllerHanafi Hisyam

3 Answers

0
votes

try to dismiss all presented controller it may work for you

DispatchQueue.main.async {
    self.view.window!.rootViewController?.dismiss(animated: true, completion: {
       print("All controller dismissed successfully..")
    })
}
0
votes

Try This

self.presentingViewController?.dismiss(animated: true, completion: nil)
0
votes

Hmm. Your update helps although I'm still not sure what's happening. My guess is that you are setting login as root, then presenting home. But I'm not sure where or what you are trying to dismiss. If it's on the login then there is nothing to dismiss as it's the root view controller.

My suggestion would be to reconfigure the storyboard. Because you are manually presenting the view controllers I'm going to assume that the storyboard doesn't contain any segues between the controllers. I'd suggest adding the segues and using them.

I'd set the home view controller as the initial view controller and set the Main storyboard as the storyboard to load in the apps settings. Then all the code to load the storyboard and set the root view controller can be removed.

Next I'd make a manual modal segue from the home view controller to the login view controller. Then in the viewDidAppear of the home view controller I'd add the code to decide if a login was needed and to them perform the login segue.

Then in the login view controller you can do a dismiss and it will remove the model login view.

This is but one of many ways to do this, but it's pretty much the simplest to get you going. You don't need any code to load or set root view controllers or anything else. It just lets the storyboard do the work for you.