I have a pointer to a UIView
. How do I access its UIViewController
? [self superview]
is another UIView
, but not the UIViewController
, right?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
@andrey answer in one line (tested in Swift 4.1):
usage:
More type safe code for Swift 3.0
To get reference to UIViewController having UIView, you could make extension of UIResponder (which is super class for UIView and UIViewController), which allows to go up through the responder chain and thus reaching UIViewController (otherwise returning nil).
A little bit late, but here's an extension that enable you to find a responder of any type, including ViewController.
For debug purposes only, you can call
_viewDelegate
on views to get their view controllers. This is private API, so not safe for App Store, but for debugging it is useful.Other useful methods:
_viewControllerForAncestor
- get the first controller that manages a view in the superview chain. (thanks n00neimp0rtant)_rootAncestorViewController
- get the ancestor controller whose view hierarchy is set in the window currently.Alas, this is impossible unless you subclass the view and provide it with an instance property or alike which stores the view controller reference inside it once the view is added to the scene...
In most cases - it is very easy to work around the original problem of this post as most view controllers are well-known entities to the programmer who was responsible for adding any subviews to the ViewController's View ;-) Which is why I guess that Apple never bothered to add that property.