Given a view, how do I get its viewController?

后端 未结 13 751
北海茫月
北海茫月 2020-11-28 02:00

I have a pointer to a UIView. How do I access its UIViewController? [self superview] is another UIView, but not the

13条回答
  •  我在风中等你
    2020-11-28 02:44

    More type safe code for Swift 3.0

    extension UIResponder {
        func owningViewController() -> UIViewController? {
            var nextResponser = self
            while let next = nextResponser.next {
                nextResponser = next
                if let vc = nextResponser as? UIViewController {
                    return vc
                }
            }
            return nil
        }
    }
    

提交回复
热议问题