Is there a built-in way to get from a UIView
to its UIViewController
? I know you can get from UIViewController
to its UIView
via [self view]
but I was wondering if there is a reverse reference?
Ios – Get to UIViewController from UIView
cocoa-touchiosobjective cuiviewuiviewcontroller
Related Topic
- Ios – Loaded nib but the ‘view’ outlet was not set
- Objective-c – Add UIView on UIViewController
- Ios – Passing data between view controllers
- Ios – UIView frame, bounds and center
- Ios – Should IBOutlets be strong or weak under ARC
- Ios – How to set cornerRadius for only top-left and top-right corner of a UIView
- Ios – Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
- Ios – Proper practice for subclassing UIView
Best Answer
Using the example posted by Brock, I modified it so that it is a category of UIView instead UIViewController and made it recursive so that any subview can (hopefully) find the parent UIViewController.
To use this code, add it into an new class file (I named mine "UIKitCategories") and remove the class data... copy the @interface into the header, and the @implementation into the .m file. Then in your project, #import "UIKitCategories.h" and use within the UIView code: