How Swift obtains the Viewcontroller that the view belongs to

Source: Internet
Author: User

It is easy to get view from VC, but in some cases we need to get VC from view reverse.

But on some special occasions, the Cocoa Library helps us to be thoughtful, such as when customizing the View transition Animation:

funcanimateTransition(transitionContextUIViewControllerContextTransitioning{}

When the system callbacks our Animatetransition method, it passes in a context parameter, from which we can easily get the toview,fromview that participate in the animation and their corresponding VC:

let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!

But not all situations will help you think so thoughtful, so sometimes you need to find its VC from the view, the online obj-c code example:

-(uiviewcontroller  *) Viewcontroller {for  (uiview  * next  = [ Span class= "Hljs-keyword" >self Superview]; next ; next  = next . Superview) {uiresponder  *nextresponder = [next  Nextresponder]; if  ([Nextresponder iskindofclass:  [uiviewcontroller  class ] ") { return  (uiviewcontroller  *) Nextresponder; }} return  nil ;}  

Let's change it to the swift version, here's the first attempt:

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 } 

The method above can do its job well, but the compiler has a warning that the syntax for this will be removed in a future swift version, so we'll turn it into a code without warning:

func controller2(view:UIView)->UIViewController?{        next:UIView? = view        repeat{            ifnext?.nextResponder() where nextResponder.isKindOfClass(UIViewController.self){                return (nextResponder as! UIViewController)            }            nextnext?.superview        }whilenextnil        returnnil    }

As you can see, there is no warning, but the code is a little bit more.

Finally, we can replace the starting code with the following:

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

How Swift obtains the Viewcontroller that the view belongs to

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.