1

Basically I want to get a list of action targets for a UIButton. I have gone through this and my question is slightly different because I do not know what the target is. All I have is a UIButton object. So here's what I did to capture all action targets.

Inspired by below method which works where I get firstResponder object as valid pointer.

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

I used class-dump on UIKit to see UIWindow class and I found firstResponder as below.

 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIWindow : UIView {
  @package
    UIResponder             *_firstResponder;
}

Then I checked UIControl which via class-dump as

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIControl : UIView {
  @package
     NSMutableArray* _targetActions;
 }

So here's what I try to do and it crashes.

    NSMutableArray *arr = (NSMutableArray*)[((UIControl*)btn) performSelector:@selector(targetActions)];
    NSLog(@"%@",arr);

Sounds like conspiracy against me. But more likely I am goofing up some thing. Does any know how to access targetActions Array of UIControl?

EDIT: Here's the error message -

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-    [UIRoundedRectButton targetActions]: unrecognized selector sent to instance 0x1c0ab0'

Any help appreciated.

Community
  • 1
  • 1
TorukMakto
  • 2,066
  • 2
  • 24
  • 38
  • You didn't say what the error message is from the crash. Presumably the selector doesn't exist? – Ross Bencina Aug 17 '13 at 05:24
  • @Ross - Here's the error - *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton targetActions]: unrecognized selector sent to instance 0x1c0ab0' – TorukMakto Aug 17 '13 at 05:27

1 Answers1

2

According to the UIControl documentation, the message to send to get a list of targets is allTargets not targetActions as you suggest. The rest of the solution is in the accepted answer to How to get UIButton Target, Action and Control events?

A debugging technique you can use when you don't know what you're doing is to use respondsToSelector to check whether you're sending a message that the object can respond to: when to use respondsToSelector in objective-c

Community
  • 1
  • 1
Ross Bencina
  • 3,822
  • 1
  • 19
  • 33
  • I don't need to actually invoke the SEL but I want to record the name of the selector. Eg: If that button was tied to a function "OnTouchMyButton", when TouchUpInside event occurs, then I just want the name of that function which I want to record in a file. – TorukMakto Aug 17 '13 at 05:31
  • Let me try allTargets and getback. Thanks. – TorukMakto Aug 17 '13 at 05:33
  • 1
    @Bugivor first you get the targets, then you get the selector names for each target with actionsForTarget:forControlEvent: I suggested using respondsToSelector because you seem to be trying to reverse engineer what selectors you can send to an object. You need to read the documentation. – Ross Bencina Aug 17 '13 at 05:34
  • It worked/ +1 and Accepted the answer. I have 2 more doubts though? Appreciate if you know - 1) Why can't I get targetActions the way I get firstResponder? 2) forControlEvents - documentation says only 1 constant is allowed at time. How can I pass a generic value so that I get actions for all control events? – TorukMakto Aug 17 '13 at 05:47
  • UIControlEventAllEvents and UIControlEventAllTouchEvents for 2 but touchupinside works.... – TorukMakto Aug 17 '13 at 05:52
  • 1
    @Bugivore presumably firstResponder is a valid (but private) selector, that's why it works. As you saw there is no allTargets selector. See also: http://stackoverflow.com/questions/1823317/get-the-current-first-responder-without-using-a-private-api – Ross Bencina Aug 17 '13 at 06:08
  • Ahh - now I get it.. firstResponder is not a selector in UIWindow but it is in UIView and UIWindow inherits from UIView. Which makes me do lot of monkey work. Is there any way to access private variables(not exposed as property) of any object? (Just like _targetActions)? – TorukMakto Aug 17 '13 at 06:15