4

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

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?)

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?

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?

additional information:

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.

Community
  • 1
  • 1
mkc842
  • 2,361
  • 2
  • 26
  • 38
  • u can forward UIbarbutton action ,by giving the ur ViewController instance in target field.example: mycontroller is the instance of ur ViewController then :`UIBarButtonItem *urItem=[[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStyleBordered target:mycontroller action:@selector(mycontroller's function:)];` – Vasu Ashok May 15 '13 at 04:21
  • but the bar button's selector is not implemented in the corresponding view controller, and i believe that the target has to implement the selector, right? there is a different target. maybe that's ridiculous, but i'd like to understand how to make this forwarding work. – mkc842 May 15 '13 at 04:29
  • **target- The object that receives the action message. action- The action to send to target when this item is selected.** as per Apple Document we can give target ,where we want to receive our action . of course that is that class ,our selector Function should implement – Vasu Ashok May 15 '13 at 04:38
  • i understand that i've set up an unconventional, maybe ridiculous, bar button. but my goal is to improve my understanding of xcode. in this case, i want to understand forwardingTargetForSelector:, to understand whether objects can have public methods that receive parameters from other objects, and to see/understand any clever solutions to the problem i have presented. – mkc842 May 15 '13 at 04:51
  • Why is your button receiving messages it can't handle? – jscs May 15 '13 at 05:18
  • @JoshCaswell please see additional info in question – mkc842 May 15 '13 at 05:41

1 Answers1

2

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.

It can't be a property in the @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 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: 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.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • okay, cool, that clarifies the matter for me. none of this is necessary. i just want to understand the [practical] limits of the language. if you want to look at my question update and the link to another question therein, that'd be cool. thanks. – mkc842 May 15 '13 at 05:47