tags:

views:

46

answers:

2

Hi,

I want to build a selector from a class method.

I'm doing it this way:

NavigationTreeActionHandler* handler=[NavigationTreeActionHandler self];
NavigationTreeNode* bombsNode=new NavigationTreeNode("Bombs","bigbomb.tif"
     ,handler,@selector(BigBombButtonPressed:));

I need to pass to NavigationTreeNode the target and the selector to the target method. I try to get the target using the self property of the class object (Don't know if htis is the correct way to do it). Then I get the selector for the class method I want to call on the class.

Everything compiles ok but it fails when I use:

[[handler class] instanceMethodSignatureForSelector:selector];

I get a nil and don't really know why... could anybody help please?

Thanks in advance, Olson.

A: 

A few suggestions:

  • [NavigationTreeActionHandler self] will work fine to get the class object, but I would declare handler as type id instead of NavigationTreeActionHandler*, because it's not a reference to an instance that type

  • [handler class] is redundant; handler is already the class object.

  • instanceMethodSignatureForSelector: is only going to return nil if handler does not implement the selector. Verify your spelling etc., and try throwing in an NSLog to verify what you're receiving:

    NSLog("handler = %@ sel = %@", handler, NSStringFromSelector(selector));

But I'm unclear on what you're trying to do with instanceMethodSignatureForSelector: in the first place. If you're just trying to call the class method, wouldn't [handler performSelector:selector]; do what you want?

David Gelhar
A: 

Thanks for the answer.

Well I'm still getting nil... I have been playing a lot with this without any luck. And I'm pretty sure the selector is implemented (see below). Here is the code I'm using:



@interface NavigationTreeActionHandler : NSObject 
{
}
+ (void) BombGroupButtonPressed: (id) obj;
+ (void) BombLittleButtonPressed: (id) obj;
+ (void) BigBombButtonPressed:(id) obj;

@end

//And this is the client code
id handler=[NavigationTreeActionHandler self];

NavigationTreeNode* rootNode=new NavigationTreeNode("Root",0,nil,nil);
//This is what returns 
sig = [handler instanceMethodSignatureForSelector:@selector(BigBombButtonPressed:)];

The [handler instanceMethod... ] is not something I'm using my self, but from a framework (cocos2d), so I must stick to it (Just checking why cocos2d breaks there).

Thanks in advance.

Anoide
Hi again,Ok I think I know what's hapenning. Does instanceMethodSignatureForSelector work with class methods?I just added a instance method and to the NavigationTreeActionHandler and it worked ok :S.Thanks in advance.
Anoide
Hi again,More digging the correct way to do this is use methodSignatureForSelector that works for target class and instace methods and class methods.Thanks for the help.
Anoide