Better way of calling a function in another class?

Apprentice
Posts: 13
Joined: 2008.05
Post: #1
hello,

I've got an application delegate. This delegate dynamically creates an NSView (subclass) object and all works fine.

Whenever the user now hits a key, I can evaluate the event using keyDown in the NSView subclass. Is it possible to call a method of the application delegate from within the NSView keyDown method? For example with NSInvocation or function pointers?

Here again what I want to do (pseudocode):

My AppController (application delegate) creates a view and has a method called myKeyDownMethod.

The View is an object of a subclass from NSView.
In this NSView subclass the keyDown message is being invoked whenever the user hits a key.

Within keyDown I want to call myKeyDownMethod.


I thought about using functions pointers (like I would do in C++)...but did not find any obj-c approach for this...

for now my approach is this - what I actually dont like:

when I create the view I tell the view about the appController using a pointer in the NSView subclass.

... MyView : NSView {
NSObject* controller;
}

and then in the keyDown method:

...
[controller myKeyDownMethod];
...

This works fine, but I don't like this approach.
I would like to do something like this:

... MyView : NSView {
void* keyDownPointer;
}

and then in the keyDown method:

...
keyDownPointer();
...

This way my NSView subclass does not have to know about my AppController class.


Any suggestion would be great!


brush
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #2
Personally, I'd be perfectly happy letting my view know about my app delegate. It's pretty standard to pass addresses around as needed.

To send a message directly to your app delegate you will have to have the address to it no matter what. If you don't want to reference your app delegate's header (for whatever reason) then you might be able to use performSelector depending on the complexity of the message, otherwise NSInvocation will work for anything more complex, but I think NSInvocation is a messy overkill. You'll still need to pass in an id for the app delegate to the view so you have an address to send a message to.

The only other way to communicate without an address or knowledge of the receiver is with an NSNotification.

[edit] You *could* simply pass in a function pointer to a C function in your app delegate if you really wanted to I suppose, but that kind of defeats the flexibility of Obj-C...
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #3
you could do something like this:
(untested pseudo code)

Code:
//// MyView.h

@interface NSObject (ViewEventDelegateMethods)
- (void)keyDown:(NSEvent*)event;
...
@end

@interface MyView : NSView
{
    id eventDelegate;
    ...
}

...
    
@property (assign) eventDelegate;

@end

//// MyView.m

- (void)keyDown:(NSEvent*)event
{
    [eventDelegate keyDown:event];
}

//// AppController.m

- (id)init
{
    myView = ....
    [myView setEventDelegate:self];
}

- (void)keyDown:(NSEvent*)event
{
    // event handling code
}

You could get more fancy with a protocol or something....

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #4
Oh yeah, there are several different ways to skin this cat, but they all require an address and extra code. I would prefer the delegate route like you suggest rather than NSInvocation though. I still say it's easier to just let the view know about the app delegate and be done with it.

Or maybe even reconsider having any key processing in the app delegate in the first place and just let the view handle every relevant key press that it receives.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2008.05
Post: #5
okay....thank you both!

I will do it the way I already had implemented it - what seems to be like reubert's way...this way I won't have to reference my app delegate's header...

@ AnotherJake: I've looked at samples of NSInvocation...although it is not that hard to unterstand, there would be much code to write, I think.

Again: Thank you! Smile


Greetings,
brush
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #6
brush Wrote:NSInvocation...although it is not that hard to unterstand, there would be much code to write, I think.

Exactly.

Messiness aside, to me NSInvocation is at the heart of the power of Obj-C -- messaging.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Writing an ObcJ function/calling it from C++ Jones 20 8,609 Nov 21, 2006 06:32 PM
Last Post: OneSadCookie
  Problem drawing pictures after calling RunApplicationEventLoop petr6534 5 4,402 Feb 8, 2005 06:58 PM
Last Post: petr6534