I have several view controllers inherited from UIViewController. The views are embedded in Tab bar controller. I implement a custom view transition controller so that view can be switched when I tap on tabbar item. Is it possible that I can know what exact the toVC and fromVC is so that I can perform different view transition? It seems that as! does not do the trick. The variables are not being set to true when I use as?. What is correct way to do casting or any other way to check what view it is? Thanks
func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
var detail = false
var item = false
if var controller = toVC as? DetailViewController {
detail = true
}
if var controller = toVC as? ItemViewController {
item = true
}
...
return threeDAnimationController
}
One potential problem that you might be running into is having your View Controllers pushed into an UINavigationController, therefore you would have to figure out what is the visible view controller. I have this handy extension:
extension UIViewController {
var contentViewController: UIViewController? {
if let navigationController = self as? UINavigationController {
return navigationController.visibleViewController
} else {
return self
}
}
}
You can incorporate it into your code like this:
func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
var detail = false
var item = false
if var controller = toVC.contentViewController as? DetailViewController {
detail = true
}
if var controller = toVC.contentViewController as? ItemViewController {
item = true
}
...
return threeDAnimationController
}