在VC代码无效后删除VC

时间:2016-09-03 05:29:23

标签: ios objective-c uiviewcontroller

与Instagram类似,如果用户已通过检查NSUserDefault登录,我的应用程序会将Tabbar VC显示为根VC。我有以下代码处理注销以返回登录屏幕

    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

基本上LOGIN_SCREEN_SHOWN在LoginVC的viewWillAppear中设置为true,表示我们已从LoginVC中删除。

我的所有VC都有打印输出的deInit代码。我发现如果LOGINSCREENSHOWN == TRUE,dismissViewControllerAnimated正在做我想做的事情并在我的所有VC打印中调用所有deInit代码

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

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

但是,在我提供Login VC并尝试从LoginVC之后删除VC的“else”下的代码不会给我打印输出,表明代码没有按照我的意愿去做。有人可以指出我正确的方向

---- ---- UPDATE

这是Ketan Parmar的最终代码。我修改了转换类型和持续时间来模仿dismissViewControllerAnimated动画

        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
        }

1 个答案:

答案 0 :(得分:3)

你应该在appdeledate中管理这个问题,例如首先检查用户是否已登录(用户默认或其他)?如果是,则不执行任何操作,因为您已将tabBarVC设置为storyboard中的初始viewcontroller。现在,如果没有,则表示您要显示loginViewController,然后实例化loginViewController并将其设置为窗口的rootviewcontroller。并在成功登录后通过segue或tabbarVC显示instantiate。不是吗。

更新:

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

这么多代码只需要将loginVC设置为rootVC。​​

更新2:

动画

在设置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)