forwardingTargetForSelector: 如何工作? [英] How does forwardingTargetForSelector: work?

查看:24
本文介绍了forwardingTargetForSelector: 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIBarButtonItem.当它收到无法处理的消息时,我希望它将该消息转发到特定的视图控制器.

I have a UIBarButtonItem. When it receives a message it cannot handle, I want it to forward that message to a particular view controller.

我认为我可以使用条形按钮项的 forwardingTargetForSelector: 方法来完成此操作,但显然在 UIBarButtonItem 类型的对象上找不到这样的属性.(术语点:这是否意味着 forwardingTargetForSelector: 是私有属性?等等,我想我很困惑......末尾带有冒号的方法不是属性......所以你能公开一个方法(比如getter/setter)来传递参数吗?)

I thought I might be able to accomplish this using the bar button item's forwardingTargetForSelector: method, but apparently no such property is found on objects of type UIBarButtonItem. (Point of terminology: Does that mean forwardingTargetForSelector: is a private property? edit: Wait, I think I'm confused... methods with a colon at the end aren't properties... so can you ever make public a method (like a getter/setter) to which parameters are passed?)

这是否意味着为了设置 forwardingTargetForSelector: 的值,我必须在要设置的对象的 .m 文件中进行设置?这意味着我必须对 UIBarButtonItem 进行子类化?

And does that mean that in order to set the value of forwardingTargetForSelector: I must do it from within the .m file of the object for which I want to set it? Which would mean that I would have to subclass my UIBarButtonItem?

如果是这样,为什么这不是 NSObjects 的公共属性?

And if so, why is this not a public property of NSObjects?

此外,实现我的转发目标的最佳方法是什么,最好避免子类化?

And moreover, what's the best way to achieve my forwarding goal, preferably avoiding subclassing?

附加信息:

这一切都源于我倾向于重复使用单个动作来响应按下相同按钮的各种情况.该操作目前包含在我的委托中(参见 我应该如何实施 [几乎] 在各种 VC 中使用的相同动作?(答案:使用类别))并且不同之处仅在于它应该向实例化发送动作的按钮的视图控制器发送一个 presentViewController 消息.因此,在操作中,我可以向 sender 发送一个presentViewController 消息,它是按钮的一个实例,我希望能够将该消息转发到创建该实例的视图控制器按钮,如果我在每个按钮在其各自的视图控制器中实例化后立即设置它的转发属性,我就可以做到这一点.

It all stems from my inclination to reuse a single action in response to various instances of an identical button being pressed. The action is currently contained in my delegate (see How should I implement [almost] identical actions used throughout various VCs? (Answer: use a category)) and varies only in that it should send a presentViewController message to the view controller that instantiated the button that sent the action. Thus, in the action, I can send a presentViewController message to sender, which is an instance of the button, and I want to be able to forward that message to the view controller that created that instance of the button, which I can do if I set each button's forwarding property immediately after it is instantiated in its respective view controller.

我希望避免使用为什么"只是为了让问题更简短,但是你去吧.

I hoped to avoid the "why" just to make the question shorter, but there ya go.

推荐答案

forwardingTargetForSelector: 并不是真正的属性;这更像是当实例不响应消息时运行时向实例提出的问题.

forwardingTargetForSelector: is not really a property; it's more like a question the runtime asks an instance when the instance doesn't respond to a message.

不能成为 @property/ 意义,因为每个选择器可能有不同的目标;它背后需要有一个映射.声明的属性不是这样工作的.

It can't be a property in the @property/declared-property sense, because each selector could have a different target; there would need to be a mapping behind it. That's just not how declared properties work.

UIBarButtonItem 继承自 NSObject,它与所有其他方法一起继承了这个方法,但是你不能从外部设置"选择器的转发目标实例(不创建一些其他机制来允许您这样做,无论如何 - 可能,但默认情况下不可用).

UIBarButtonItem descends from NSObject, and it inherits this method along with all the others, but you can't "set" the forwarding target for a selector from outside an instance (without creating some other machinery to allow you to do so, anyways -- possible, but not available by default).

为了使用这个方法,是的,你必须在进行转发的类中实现它.这确实意味着子类化.这也意味着转发实例需要引用它转发到的对象;这需要精心设计.

In order to utilize this method, yes, you have to implement it in the class that is doing the forwarding. This does indeed mean subclassing. It also means that the forwarding instance needs to have a reference to the object to which it is forwarding; this requires careful design.

forwardingTargetForSelector: 绝对不是实现目标的正确方法.一般来说,实际上,它有点深奥.

forwardingTargetForSelector: is all but certainly not the correct way to achieve whatever your goal is. In general, in fact, it's a bit esoteric.

我不确定您要解决的究竟是什么问题(制作一个按钮转发它不响应的消息"仍然很笼统——特别是,为什么有必要吗?),所以很难更精确.

I'm not sure exactly what problem you're trying to solve ("making a button forward messages it doesn't respond to" is still rather general -- in particular, why is that necessary?), so it's hard to be more precise.

这篇关于forwardingTargetForSelector: 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆