Reputation: 8977
I have a UIViewController in which it adds in a UIView X as a subview. From my X class is it possible to get access to UIViewController A?
Upvotes: 0
Views: 764
Reputation:
If you aren't going to upload this to the App Store, you can use a private method of UIView.
@interface UIView(Private)
- (UIViewController *)_viewControllerForAncestor;
@end
// From the X Class
UIViewController *vc = [self _viewControllerForAncestor];
[vc doStuff];
Upvotes: 0
Reputation: 9923
You could hack something, but there is nothing that exists in the SDK, and for good reason.
I agree with Kurt, and will add that usually it's the view controller itself to which you want to maintain a reference, thereby maintaining a reference to its view. Code within a UIView subclass proper shouldn't concern itself whatsoever with whether it's owned by a VC, or for that matter is just a subview of another view; its responsibility is to maintaining its own house. Case in point, you write a view for the iPhone then later port to iPad. If it references an iPhone-specific VC, you must work to weed out all of that functionality when you include it in an iPad-type VC.
The idea is that UIKit is architected in layers of abstraction. One characteristic of the LoA paradigm is that layers should generally interact with other sibling layers, and those below. But it's generally not necessary (or possible) for a layer to look upwards to a more abstract concept. UIViewController and its variants are as abstract as classes get in UIKit. They are at the top of the food chain, as it were - no other classes consume their functionality. Requiring pointers to point only sideways or downwards also helps to prevent circular references.
Upvotes: 1
Reputation: 1321
Cocoa provides no property to access it. Truthfully, if you are accessing it, you're likely violating some priciples of MVC.
Upvotes: 4