views:

111

answers:

2

I'm looking at IKImageDemo supplied by Apple, the rotate round-slider is linked to a setRotation: method in the FirstResponder. However, none of the objects in the project seem to HAVE such a method, and yet the code works.

I'm trying copy this into my own project, and MY FirstResponder doesn't have a setRotation: method, so I'm not sure where it lives. Google has been unhelpful...

thanks.

+1  A: 

Well, the first responder in the app happens to be an instance of IKImageView. IKImageView responds to the setRotation: selector (which can be seen by passing respondsToSelector:@selector(setRotation:) to any instance of IKImageView), although I cannot find where in documentation it mentions the setRotation: method

RJ
A: 

First Responder methods aren't magic. What happens when a message is sent to the first responder is that the app's current first responder (this is usually the focused view/control) is asked whether or not it implements the method. If it does, the method is called. If it doesn't, the next responder up the chain is asked, and so on until the top level (the NSApplication instance) is reached. The object must actually implement the method for it to be called, it can't just declare it.

In this case IKImageView implements -setRotation: as a private method. This means that the method is present (which is why the IKImageView accepts the message sent to the First Responder) but its use is not documented or supported. It seems odd that Apple would ship an example using a private method but there you go. It's definitely the case that sometimes methods are accidentally left out of the public headers when their use is supported, however it's generally wise to avoid private methods unless someone from Apple has specifically told you it's OK to use one.

You can generate headers for all methods of an Objective-C object, including private methods, from the binary using class-dump.

IKImageView has a public method -setRotationAngle: which is probably the way to go if you want to change the rotation.

Rob Keniger
How do I link to it in IB if it doesn't show up in the list? setRotationAngle doesn't appear in the list of things to link to, possibly because it's not a normal function, it's a magical setter that comes from the rotationAngle property? and when I link a (round) slider to a method like that, it automatically plugs the value of the slider into the method?
Brian Postow
Also, is that why in the firstresponder in the demo, there's a bunch of methods listed under "MainMenu.nib" in addition to the normal "System Defined"? My project doesn't have any of those, and I'm not sure how to make them, or how to point them at actual code...
Brian Postow