FirstResponder 方法从何而来?
我正在查看 Apple 提供的 IKImageDemo,旋转圆形滑块链接到 FirstResponder 中的 setRotation: 方法。然而,项目中的任何对象似乎都没有这样的方法,但代码仍然有效。
我正在尝试将其复制到我自己的项目中,而我的 FirstResponder 没有 setRotation: 方法,所以我不确定它位于哪里。谷歌一直没有帮助...
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,应用程序中的第一响应者恰好是 IKImageView 的实例。 IKImageView 响应 setRotation: 选择器(可以通过将 respondsToSelector:@selector(setRotation:) 传递给 IKImageView 的任何实例来看到),尽管我找不到文档中提到 setRotation: 方法的位置
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
急救人员的方法并不神奇。当消息发送到第一响应者时,会询问应用程序当前的第一响应者(这通常是焦点视图/控件)是否实现该方法。如果是,则调用该方法。如果没有,则询问链上的下一个响应者,依此类推,直到到达顶层(
NSApplication
实例)。对象必须实际实现要调用的方法,而不能只是声明它。在本例中,
IKImageView
将-setRotation:
实现为私有方法。这意味着该方法存在(这就是 IKImageView 接受发送到第一响应者的消息的原因),但其使用未记录或支持。苹果会使用私有方法发布一个示例,这似乎很奇怪,但你就知道了。有时,当支持方法的使用时,方法会被意外地排除在公共标头之外,但通常明智的做法是避免使用私有方法,除非 Apple 的某人特别告诉您可以使用私有方法。您可以使用 class-dump.
IKImageView
有一个公共方法-setRotationAngle:
如果您想更改旋转,这可能是可行的方法。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.我找到了解决这个烦恼的方法。即使在最初的 Apple 示例中,一旦您在第一响应程序中删除了 setRotation 的绑定,您就无法将其放回去,除非使用以下技巧:只需使用第一响应程序的属性检查器并添加用户定义的操作“setRotation:”输入“id”。现在,甚至 Apple 示例中 setRotation: 的第一响应者绑定中的黄色三角形也消失了,并且它也出现在我自己的 IKImageView 实例中。
I've found a way of resolving this annoyance. Even in the original Apple example, once you remove the binding for setRotation in the First Responder, you cannot put it back, unless doing this trick: simply use the Attributes Inspector for the First Responder and add a User Defined action "setRotation:" with type "id". Now even the yellow triangle in the First Responder binding for setRotation: in the Apple example disappears, and it shows up also in my own IKImageView instance.