Calling Parent Methods from Child


#1

Hi, in your OvalShop project you have the following code

- (Document *)document { return (Document *)[[[self window] windowController] document]; }

  • which I think I understand as allowing the instance of OvalView to call a method in its creator using this:

While this is very clever, I am struggling to understand the syntax/how this works - to understand this I have tried to create a simple test App (BLAppDelegate) that creates an instance of a simple class (shape) - I am now trying to write code in the shape class which is instantiated by the App that calls a method in the parent App/Class and have no idea what the syntax would be… shape.m includes “BLAppDelegate.h” - and “BLAppDelegate.h and .m” have a simple method called -(void) amethod;.

So given BLAppDelegate is an NSObject with a method amethod - how do I call amethod from the class shape that is created/instantiated by BLAppDelegate ?

Thanks
Ian

Full code of my test app is :

[code]//
// BLAppDelegate.h
// testApp2
//

#import <Cocoa/Cocoa.h>
@class shape;

@interface BLAppDelegate : NSObject {
shape *ashape;
}

@property (assign) IBOutlet NSWindow *window;
-(void)amethod;

@end

//
// BLAppDelegate.m
// testApp2
//

#import “BLAppDelegate.h”
#import “shape.h”

@implementation BLAppDelegate

-(id)init {
if(self = [super init]) {
ashape = [[shape alloc] init];
}
return self;
}

-(void)awakeFromNib {
[super awakeFromNib];
[ashape doSomething];
}

  • (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    }

-(void)amethod {
NSLog(@“amethod called”);
}

@end

//
// shape.h
// testApp2
//

#import <Foundation/Foundation.h>

@interface shape : NSObject

@property NSPoint location;
-(void)doSomething;

@end

//
// shape.m
// testApp2
//

#import “shape.h”
#import “BLAppDelegate.h”

@implementation shape
@synthesize location;

-(id)init {
if(self = [super init]) {
location.x = 0;
location.y = 0;
}
return self;
}

-(void)doSomething {
// call BLAppDelgate/parents “amethod”
??? What do I put here ???
}

@end
[/code]


#2

I don’t know why you want to do this, but this is something easy to do.

You can pass the creator as a delegate to the shape; then the shape can invoke any method on it.

Here is a quick sketch-up.

BLAppDelegate.m

#import "BLAppDelegate.h"
#import "shape.h"

@implementation BLAppDelegate
-(id)init {
    if(self = [super init]) {
        ashape = [[shape alloc] initWithDelegate:self];
    }
    return self;
}
...

Shape.m

#import "shape.h"
#import "BLAppDelegate.h"

// keep delegate private
@interface Shape ()
{
     id _delegate;   // don't retain
}
@end

@implementation shape
@synthesize location;

-(id)initWithDelegate:(id)delegate {
    if(self = [super init]) {
        location.x = 0;
        location.y = 0;
        _delegate = delegate;  // don't retain
    }
    return self;
}

-(void)doSomething {
    // call BLAppDelgate/parents "amethod"

    [_delegate amethod];
}

For an object to directly call a method on an other object, the object needs to have a pointer to that object.

A Bar object wants to invoke a method on a Foo object:

@implementation Bar
...
Foo *foo;
...
foo = ...    // must be initialized with something like a Foo
...
[foo pleaseHelpMe:self]
...

#3

Thanks this a great answer… the reason to do it was that I was working on another program with a object that was a NSView on a window and the Window had a Label which I wanted to populate with the co-ordinates of mouse clicks etc… I managed to do it with NSNotification as I could not find a way of a child class calling a class for its parent… clearly this is how you do it ! Many thanks.