Swift)UIView에서 자신을 띄운 ViewController를 얻는 법

UIKit, UIView

Featured image

@

extension UIView
{
    /// UIView가 속해있는 ViewController를 가져옴
    func findViewController() -> UIViewController? {
        if let nextResponder = self.next as? UIViewController {
            return nextResponder
        } else if let nextResponder = self.next as? UIView {
            return nextResponder.findViewController()
        } else {
            return nil
        }
    }
}