2008 03 29Target+action from a Javascript standpoint
onload, onclick, xmlHttpRequest.onreadyStateChange
or setTimeout, setInterval
, you'll have to supply a function to know what's happening. And here lies the catch : it's just a function. If you want to use one of your objects' method, you'll have to wrap it in a closure :
// Our custom object function MyObject() {} MyObject.prototype = { notifyWhenXmlHttpRequestChanged : function (x) { // do stuff when receiving notification } } // instance of our custom object var myObject = new MyObject // Use an instance method as notification xmlHttpRequest.onreadystatechange = function (x) { myObject.notifyWhenXmlHttpRequestChanged(x) }
That's because Javascript loves functions : they are first class, meaning they get treated the same as objects, numbers and strings — you can create, modify, delete them anytime. In Objective C, objects reign supreme and functions don't get any love. Therefore ObjC always notifies a method (action) of an object instance (target). Our onreadystatechange
notification would look like that :
[xmlHttpRequest setTarget:myObject]; [xmlHttpRequest setAction:@selector(notifyWhenXmlHttpRequestChanged:)];
@selector is the ObjC way of specifying a method name. It's still just a string.
That's it. Supply an instance and a method name, and ObjC will know what to call. Target/Action is used in every NSControl
and is the standard way to react to an UI event : when being clicked, an NSButton will call its target/action. This is how you're notified of a click, you can use any method of any object to act on it.
Interface Builder and one limit
In IB, target and action are setup both at the same time by right-dragging from an interface element (button, checkbox, …) to an object instance. (Your instances are listed in the window containing File's Owner, First Responder, …) You can therefore bind a button of you window to ApplicationController.clickedSomeButton:
.
Unfortunately, you can't use keyPaths to set your target object, so you can't use ApplicationController.someObject.clickedSomeButton:
. There's nothing preventing it (you can do it in code), but it's not available in IB.
Objective-C from Javascript
- KVC and KVO from a Javascript standpoint
- ObjC protocols from a Javascript standpoint
- Delegates from a Javascript standpoint
- Target+action from a Javascript standpoint
- Outlets from a Javascript standpoint
- Dynamic method over* from a Javascript standpoint
- The Responder Chain from a Javascript standpoint
- PerformSelector from a Javascript standpoint
- NIBs from a Javascript standpoint
- Events from a Javascript standpoint