views:

311

answers:

3

Is there an equivalent function to SendMessage in the Mac OS?

+3  A: 

Ironically, every method call in Objective-C is the equivalent of SendMessage. Objective-C is at heart a message passing system.

So you just say:

[window myMessage]

and the myMessage routine will be executed by passing myMessage to the Window object and having it process that method...

It's also possible the closer thing to what you really want to do would be to use Notifications to message between components.

If you don't have the Window object around at compile time, the compiler may complain it doesn't know if Window can handle the message you are sending. For those cases you can use:

[window performSelector:@selector(myMessage)]

There are alternate versions of this call that allow passing objects as parameters.

Kendall Helmstetter Gelner
A: 

On a higher level, if you are talking about the Carbon Event Manager, you would use the function 'SendEventToEventTarget'

See http://developer.apple.com/documentation/Carbon/Reference/Carbon_Event_Manager_Ref/Reference/reference.html

NineBerry
+1  A: 

It depends on what message you'd be sending with SendMessage(). Most events in Cocoa go through -[NSApplication sendEvent:] for example, or SendEventToEventTarget() if you wanted a lower-level version. For other messages, such as resizing, movement, etc. you would need to look at the appropriate methods of NSWindow (such as -setFrame:animated:) or NSApplication.

Generally speaking, instead of using a funnel routine and function constants as SendMessage() does, in Cocoa you just get hold of the relevant object and call its methods.

Jim Dovey