kossibox
kossibox

Reputation: 221

iphone sdk - calling a method of view's superview's viewcontroller

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

Upvotes: 3

Views: 11861

Answers (4)

holodnyalex
holodnyalex

Reputation: 791

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

Upvotes: 1

Nava Carmon
Nava Carmon

Reputation: 4533

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...

Upvotes: 4

Ben Scheirman
Ben Scheirman

Reputation: 40951

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.

Upvotes: 7

Martin Gordon
Martin Gordon

Reputation: 36389

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.

Upvotes: 4

Related Questions