简体   繁体   中英

What is the equivalent (if any) to the C++/Windows SendMessage() on the Mac?

Mac OS中有与SendMessage等效的功能吗?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM