views:

3415

answers:

5

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

+3  A: 

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.

Martin Gordon
A: 

thank you so much... how can i then access superview's methods? i have access to the sdk's default methods but not the ones i have writen myself. this is "superview".h (trying to access drawChessBoardTab)

#import <UIKit/UIKit.h>
@interface EchiquierDisplayView : UIView {
    IBOutlet UIViewController *chessPiecesDisplayViewController;
    BOOL uperLeftCellColor; //uperLeftCellColor: YES->White, NO->Black; 
}
@property (nonatomic, retain)  UIViewController *chessPiecesDisplayViewController;
-(void) drawChessBoardTab: (CGContextRef) context:(float )boardWidth;

@end

this is "currentview".h

#import <UIKit/UIKit.h>

/*
*   Les pieces seront de ce type
*/

@interface pieceDraggableImageView : UIImageView {
   CGPoint startLocation;
   CGPoint startLocationInView; 
}
-(void)correctDestinationPosition;
@end

i think i should tell the views somehere that they are related... can you help me thanx

kossibox
Please add this to your original question, not as an answer.
mahboudz
+2  A: 

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.

Ben Scheirman
+3  A: 

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

Nava Carmon
hello,i've tried your sugestion. i can now access [theSuperview correctIt];and it compiles correctly. But i can see no correction. even not the NSlogs. Do you think of anything i should check?? thank you again
kossibox
I think you should refresh your view by calling in correctDestinationPosition after the correction [self setNeedsDisplay:YES];
Nava Carmon
A: 

thank you so much. i'll try it and tell you if it works.

kossibox
Please add these texts as comments, not as another answer.
mahboudz