0

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
}
thsieh
  • 620
  • 8
  • 24
  • What is `var controller`? In the `if` statements, are you trying to check if `toVC` is the respective view controller––`DetailViewController` or `ItemViewController` in your two examples? – Daniel Dec 27 '15 at 07:49

3 Answers3

2

In Swift you can use the is operator to check what class it is and in objc you can use the isKindOfClass method:

In objc you can do:

if ([toVC isKindOfClass:[DetailViewController class]]) { detail = true }

In swift you can do:

if toVC is DetailViewController { detail = true }

Joe Benton
  • 3,703
  • 1
  • 21
  • 17
2

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
}
Julian J. Tejera
  • 1,015
  • 10
  • 17
1

You can just create a switch where you then perform your desire transition:

switch toVC {
    case is ItemViewController:
        // do some stuff, should already casted I think
    case is DetailViewController:
        // again do some stuff
    default: break
}

And as some other users mentioned here you could run into trouble if you wrap your viewcontroller in a Navigation or UISplitViewController so therefore just create an extension like Julian J. Tejera already mentioned in his answer. (also add the UISplitViewController as a case if you supporting iPad anytime)

Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51