Edward Hung Edward Hung - 3 months ago 10
iOS Question

Remove VC after presenting VC code not working

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
}

Answer

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)