1

Similar to Instagram, my app shows Tabbar VC as the root VC if the user already has already logged in by checking NSUserDefault. I have the following code that deals with logout to go back to login screen

    if LOGIN_SCREEN_SHOWN == true {
            self.dismissViewControllerAnimated(true, completion: nil)

        } else {
            let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("LoginVC") as! LoginVC
            self.presentViewController(loginVC, animated: true) {
                //self.removeFromParentViewController()
                self.tabBarController!.removeFromParentViewController()
                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                appDelegate.window?.rootViewController = loginVC
                LOGIN_SCREEN_SHOWN = true
            }
        }

flow

Basically LOGIN_SCREEN_SHOWN is set to true in viewWillAppear in LoginVC indicating that we have been segue'd from LoginVC.

I have deInit code with print outs in all my VC. What I am finding is that if LOGINSCREENSHOWN == TRUE, dismissViewControllerAnimated is doing what I want to do and calling all the deInit code in all my VC printing out

NavVC Deinit Successfully
NavVC Deinit Successfully
NavVC Deinit Successfully
NavVC Deinit Successfully

VC1 Deinit Successfully
VC2 Deinit Successfully
VC3 Deinit Successfully
VC4 Deinit Successfully

However, the code under "else" where I present Login VC and attempt to remove VC from after LoginVC is presented does not give me the print out indicating that the code is not doing what I'd like it to do. Could someone please point me to the right direction

----UPDATE----

This is the final code that works from Ketan Parmar. I have modified the transition type and duration to mimic dismissViewControllerAnimated animation

        if LOGIN_SCREEN_SHOWN == true {
            self.dismissViewControllerAnimated(true, completion: nil)

        } else {
            // Custom transition
            let transition : CATransition = CATransition()
            transition.duration = 0.35
            transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionReveal
            transition.subtype = kCATransitionFromBottom

            let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("LoginVC") as! LoginVC
            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.window?.layer.addAnimation(transition, forKey: nil)
            appDelegate.window?.rootViewController = loginVC
            LOGIN_SCREEN_SHOWN = true
        }
user172902
  • 3,541
  • 9
  • 32
  • 75
  • In which method you are implementing self.dismissViewControllerAnimated(true, completion: nil) ? – Vishal Sharma Sep 03 '16 at 05:38
  • Somewhat similar query: http://stackoverflow.com/questions/39159444/how-to-get-navigation-based-template-functionality-in-swift-programming/39159793#39159793 – Bista Sep 03 '16 at 05:49
  • dissmissViewControllerAnimated is implemented in VC4 with a logoutBtn – user172902 Sep 03 '16 at 06:22
  • @Mr.UB, this is un-related to that question you linked – user172902 Sep 03 '16 at 06:29
  • hmm..1 query: why do you want to use this line of code: `self.tabBarController!.removeFromParentViewController()`? – Bista Sep 03 '16 at 06:30
  • No specific reason really. I was just trying things to see if any code can successfully de-initalise the VCs that I am leaving behind – user172902 Sep 03 '16 at 07:48
  • finally you trying to do if user logged in so he/she directed to `tabView` if not he will be directed to loginView ? – vaibhav Sep 03 '16 at 08:47

1 Answers1

3

you should manage this in appdeledate something like first check that does your user is logged in(userdefaults or whatever)? If yes then do nothing because you have set your tabBarVC as initial viewcontroller in storyboard. Now, if no that means you want to show loginViewController then instantiate loginViewController and set it as rootviewcontroller of window. and after successfully login show tabbarVC by segue or by instantiate it. that't it.

Update :

    let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("LoginVC") as! LoginVC
   let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window?.rootViewController = loginVC

That much code only require to set loginVC as rootVC.

Update 2:

for animation add below code snippet before setting rootVC,

 let transition : CATransition = CATransition()
    transition.duration = 0.2
    transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft
    (UIApplication.sharedApplication().delegate as! AppDelegate).window?.layer.addAnimation(transition, forKey: nil) 
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • This is pretty much exactly what I am doing already. But the question mentioned above is that if the user has tabBarVC as the rootVC and wants to go back to loginVC because he wants to log out, the code does not cause the existing VCs to deinit – user172902 Sep 03 '16 at 06:27
  • Could you please take a look at my code and provide and example. Because as you can see in my else section, I did try and set loginvc as root vc and it didnt work. Cheers – user172902 Sep 03 '16 at 07:52
  • That works! Thanks champ. One question, there is no animation doing it this way as the login screen just appears instantly. How would I be able to do it so it has the animation of popping view controller or dismissing view controller? – user172902 Sep 05 '16 at 01:08
  • You kind sir seems very passionated! Thanks for providing such good answer so quickly. I have updated the question with your answer – user172902 Sep 05 '16 at 07:23