【发布时间】:2016-09-03 05:29:22
【问题描述】:
与 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
}
}
基本上 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,但并没有给我打印输出,表明代码没有做我想做的事情。有人可以指出我正确的方向
----更新----
这是来自 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
}
【问题讨论】:
-
你在哪个方法中实现 self.dismissViewControllerAnimated(true, completion: nil) ?
-
dissmissViewControllerAnimated 在 VC4 中使用 logoutBtn 实现
-
@Mr.UB,这与您链接的问题无关
-
hmm..1 查询:为什么要使用这行代码:
self.tabBarController!.removeFromParentViewController()?
标签: ios objective-c uiviewcontroller