Swift如何取得View所属的ViewController

前端之家收集整理的这篇文章主要介绍了Swift如何取得View所属的ViewController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

从VC取得View很容易,但有些情况下我们需要从View反向获取VC.

不过在一些特殊的场合,Cocoa库帮我们想的很周到,比如在自定义View过渡动画的时候:

func animateTransition(transitionContext: UIViewControllerContextTransitioning) { }

系统在回调我们的animateTransition方法时,会传入一个context参数,从它我们可以轻松取得参与动画的toView,fromView以及它们对应的VC:

let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!

let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!

但不是所有情况系统都会帮你考虑的这么周到,所以有时候还得需要自己从View找到它的VC,网上有Obj-c的代码示例:

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

我们来将其改为Swift版本,以下是第一次尝试:

func controller(view:UIView)->UIViewController?{
        for var next:UIView? = view;next != nil;next = next!.superview{
            if let nextResponder = next?.nextResponder() where nextResponder.isKindOfClass(UIViewController.self){
                return (nextResponder as! UIViewController)
            }
        }
        return nil
    }

上面的方法可以很好的完成其功能,不过编译器有警告,for如此的语法将会在将来的Swift版本中剔除,所以我们还是将其转成没有警告的代码:

func controller2(view:UIView)->UIViewController?{
        var next:UIView? = view
        repeat{
            if let nextResponder = next?.nextResponder() where nextResponder.isKindOfClass(UIViewController.self){
                return (nextResponder as! UIViewController)
            }
            next = next?.superview
        }while next != nil
        return nil
    }

正如你所见,虽然没有警告,但是代码要稍微多一些了.

最后我们可以把开始的代码替换如下:

let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!

let toVC = controller2(toView)!
let fromVC = controller2(fromView)!
原文链接:https://www.f2er.com/swift/323567.html

猜你在找的Swift相关文章