Image:CocoaNav icon.png

PARMANOIR

Target+action from a Javascript standpoint

In Javascript, you sometimes need to supply a function to be used as notification. Whether it be 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

2008 07 25Where in build phases is that file ?
2008 07 192Imagine clickable error messages
2008 07 18Succulent Stormhoek
2008 07 173Loving the for
2008 07 12(Parmanoir) Feed now validates
2008 07 108Telling classes from instances
2008 07 08Comma Trick
2008 07 06Using libffi
2008 07 04BridgeSupport's type and type64
2008 07 042Clickable Disabled MenuItems
2008 07 026Less bugs through compiler optimizations
2008 06 251CocoaNav JS, a light CocoaNav for Safari
2008 06 232NSWindow goodies : bottomCornerRounded, usesLightBottomGradient
2008 06 222Inspecting NSUndoManager's undo stack
2008 06 16Cocoa Regular Expressions via JavascriptCore
2008 06 15Crossing the WebKit bridge
2008 06 08Double and Triple Click
2008 06 05Photoshop-like compositing with Core Animation
2008 06 052One way binding to NSSlider
2008 05 30Threaded Core Animation, Part Deux
Image:rss.png
Image:rss.png

Powered by MediaWiki

Hi ! I'm learning Cocoa to (hopefully !) become an indie developer.

I've written software all my professional life, in C++, PHP, Javascript. I've designed websites and web interfaces. My last venture went into flames as clients were happy but didn't like paying very much.

I've had little luck in the B2B world, I'm hoping for a better future writing Mac applications.

Planet Cocoa