本文将讲述在iOS开发中如何根据某个UIView(前提是该UIView已经被加入到Controller中)来获取所属的UIViewController。javascript
作过事件响应处理的童鞋应该都或多或少听过这个类吧,UIResponder的子类有不少好比UIView、UIViewController、UIWindow甚至是UIApplication。java
再引入一个概念叫Responder Chain,即响应用户触摸的一条事件处理、响应的链,用户一旦接触屏幕,这个事件就会按照这条Responder Chain传递下去。大概的传递规则就是从视图顶层的UIView向下到UIViewController再到RootViewController再到Window最后到Application。具体如何传递我这里再也不详述,能够参阅UIResponderui
当咱们知道UIResponder是UIView和UIViewController的父类,并且他们会经过UIResponder造成一条链,那么咱们不就能够从UIView开始顺着这条链找到UIViewController了。this
啥也不说了直接看代码code
extension UIView{ /** Get the controller that this view belongs to. it will return nil if there is no controller. It won't return a object of the class in arguments, just return a object of UIViewController. If you want use your class, you should use '... as? YourViewControllerClass' to convert it to the class you need. */ func viewController(aClass: AnyClass) -> UIViewController?{ for(var next=self.superview;(next != nil);next=next?.superview){ let nextResponder = next?.nextResponder() if((nextResponder?.isKindOfClass(aClass)) != nil){ return nextResponder as? UIViewController } } return nil } }
extension
这里是Swift的提供的用于扩展某个类的写法,这里我扩展了UIView,为他加上了一个viewController方法。用一个循环先找到最底层的UIView,而后经过这个最底层的UIView顺着Responder chain去找该UIView的下一个nextResponder()
。因为Responder是UIViewController的父类,咱们经过as转换成UIViewController便可。事件
直接把上面这段代码加入你的Swift文件中,UIView就能够调用viewController()
来获取他的controller了。ip