PDA

View Full Version : Accessing instance methods of parent view from child view?




neil.b
Dec 4, 2008, 05:00 PM
I've got an instance method in my view controller that I want to call from one of the view controller's sub views.

Is there a way to do this? In Actionscript (for example) you can use parent.function but for the life of me I can't figure it out in ObjC.

Thanks



jnic
Dec 5, 2008, 04:39 AM
I've got an instance method in my view controller that I want to call from one of the view controller's sub views.

Is there a way to do this? In Actionscript (for example) you can use parent.function but for the life of me I can't figure it out in ObjC.

Thanks

You could extern the view controller you want to access and then call that from the sub view, i.e.:

extern YourViewController *yvc;

in YourViewController.h;

YourViewController *yvc;

in YourViewController.m (before you declare the @implementation); and

[yvc yourInstanceMethod];

from the sub view. (You'll need to #import YourViewController.h into YourSubView.m.)

neil.b
Dec 5, 2008, 05:56 AM
I'll give that a try, thanks :)

firewood
Dec 5, 2008, 08:58 AM
I would stick a pointer to the superview inside the subview object, and have that pointer populated by the superview on init or load. That way the superview is more in control of its own use.

.

neil.b
Dec 5, 2008, 09:07 AM
I would stick a pointer to the superview inside the subview object, and have that pointer populated by the superview on init or load. That way the superview is more in control of its own use.

.

Nice. I like that one.

:)