Reputation: 2101
i m adding a custom view to a view controller , by the following code
[bTVC addSubview:customView];
now i want to call a instance method of parent viewController in the custom view's class like this
[self.parentViewController instanceMethod];
is that possible ? how can i do that ?
Upvotes: 0
Views: 1857
Reputation: 6176
the "normal" method should be to implement a property (not retained) delegate pointing to your parent viewController, but you may prefer to use something like this:
-(MyCustomUIViewController*)findParentUIViewControllerInSuperViews:(UIView*)myView {
for (UIView* next = [myView superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[MyCustomUIViewController class]]) {
return (MyCustomUIViewController*)nextResponder;
}
}
return nil;
}
now just call:
MyCustomUIViewController* myParentViewController = (MyCustomUIViewController*)[self findParentUIViewControllerInSuperViews:self];
[myParentViewController instanceMethod];
anyway, i also recommend you to learn how delegate works, it's useful in many cases and you'll surely need it sooner or later...
Upvotes: 4
Reputation: 216
There is no addSubview method in NSViewController. You can find it in NSView. if bTVC is an instance of NSView (or a subclass of) you can get what you call parent view by sending superview to self : [self superview] or self.superview. It'll return bTVC in your example. NSViewController works differently you can read full documentation here
Upvotes: 0