John Jackson
John Jackson

Reputation: 860

How do I target a specific viewController in a custom navigationController code?

I have this custom navigationController animation I want to implement, but I want the code to be implement only when a specific viewController is on the stack.. Here is the code:

class ARNImageTransitionNavigationController: UINavigationController, UINavigationControllerDelegate {

    weak var interactiveAnimator : ARNTransitionAnimator?
    var currentOperation : UINavigationControllerOperation = .None

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.enabled = false
        self.delegate = self
    }

    func navigationController(navigationController: UINavigationController,
        animationControllerForOperation operation: UINavigationControllerOperation,
        fromViewController fromVC: UIViewController,
        toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        self.currentOperation = operation

        if let _interactiveAnimator = self.interactiveAnimator {
            return _interactiveAnimator
        }

        if operation == .Push {
            return ARNImageZoomTransition.createAnimator(.Push, fromVC: fromVC, toVC: toVC)
        } else if operation == .Pop {
            return ARNImageZoomTransition.createAnimator(.Pop, fromVC: fromVC, toVC: toVC)
        }

        return nil
    }

    func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if let _interactiveAnimator = self.interactiveAnimator {
            if  self.currentOperation == .Pop {
                return _interactiveAnimator
            }
        }
        return nil
    }
}

Now what do I add to this code to make sure it implements all its methods but only when a specific viewController is on the stack?

Upvotes: 0

Views: 673

Answers (2)

Paul Slm
Paul Slm

Reputation: 483

This a code we use to get a specific ViewController from the NavigationController stack:

class HGViewStack{
    static func findControllerForType<T>() -> T? {
         let navController: UINavigationController = HGApp.sharedApplication().keyWindow?.rootViewController as! UINavigationController
         let controller: [T] = navController.viewControllers.filter({ $0 is T }).map({$0 as! T})
         return controller.first
    }
}

You can use it in all your function defined here like that:

if let viewController: MyViewController = HGViewStask.findControllerForType() {
    // Your code
}

Unfortunately, those functions will affect all your controllers, they just will have no effect, if that what you want.

Upvotes: 0

sweepez
sweepez

Reputation: 595

If you have given individual swift classes to the view controllers in question then you can check for their file class name. Say you want this animation when pushing e.g. ProfilePageViewControllerto nav stack, then use if operation == .Push && toVC.isKindOfClass(ProfilePageViewController) . I added the remaining code in the code sample you provided.

func navigationController(navigationController: UINavigationController,
    animationControllerForOperation operation: UINavigationControllerOperation,
    fromViewController fromVC: UIViewController,
    toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
    self.currentOperation = operation

    if let _interactiveAnimator = self.interactiveAnimator {
        return _interactiveAnimator
    }

    if operation == .Push && toVC.isKindOfClass(swiftNameofVCClassFile) {
        return ARNImageZoomTransition.createAnimator(.Push, fromVC: fromVC, toVC: toVC)
    } else if operation == .Pop && fromVC.isKindOfClass(swiftNameofVCClassFile) {
        return ARNImageZoomTransition.createAnimator(.Pop, fromVC: fromVC, toVC: toVC)
    }

    return nil
}

Upvotes: 1

Related Questions