Question

hello how can i call in the current view, a method implemented in the viewcontroller of the current view's superview? can you help me please. thanx

Was it helpful?

Solution

You can add a function -(void)initWithView:(EchiquierDisplayView *)aSuperview or something like that, define a reference in your

@interface pieceDraggableImageView : UIImageView { 
CGPoint startLocation; 
CGPoint startLocationInView;
EchiquierDisplayView *theSuperview;  
} 

@property (nonatomic, retain) EchiquierDisplayView *theSuperview;

-(void)correctDestinationPosition; 
-(void)initWithView:(EchiquierDisplayView *)aSuperview; 
...
-(void)askSuperview;
@end

@implementation pieceDraggableImageView

...
-(void)initWithView:(EchiquierDisplayView *)aSuperview
{
   theSuperview = aSuperview;
}
...
-(void) correctDestinationPosition
{
   [theSuperview correctIt];
}

Now be sure to implement the function correctIt in your superview. Hopefully i understood your question right...

OTHER TIPS

Typically this is done through delegates.

Have your view interface define a protocol and a reference to some delegate. Then have your parent viewcontroller implement this protocol.

Then the parent would do this:

someView.fooDelegate = self;

then the view would do something like this:

if(self.fooDelegate != nil) {
   if([fooDelegate respondsToSelector:...]) {
      [fooDelegate performSelector:...];
   }
}

This is not compiled, but I think you get the gist.

UIViews have no knowledge of their view controllers. You will need to create a custom UIView subclass that maintains a reference to one (or potentially more than one) view controller, although doing so introduces further coupling between UIView and UIViewController.

You should consider implementing the method in the superview's or view's class rather than implementing it in a view controller.

Here is another way:

SuperviewsViewController *controller = self.superview.nextResponder;

if (controller && [controller isKindOfClass:[SuperviewsViewController class]])
{
    [controller method];
}

It should work in most cases.

Apple's UIResponder Reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top