Class method contains selector call to a method of another class

So I created a class CommonMethods with class method:

+ (CCMenu *)createMenu:(NSString *)menuName atPosition:(CGPoint)position {
    CCMenuItemImage *menuBlock = [CCMenuItemImage itemWithNormalImage:menuName selectedImage:menuName target:self selector:@selector(choose:)];
    CCMenu *menuBlockMenu = [CCMenu menuWithItems:menuBlock, nil];
    menuBlockMenu.position = position;
    return menuBlockMenu;
}

Now in my MainClass which contains choose: method, I create a menu:

CCMenu *regularBlockMenu = [CommonMethods createMenu:kbRegularBlock atPosition:position];
[self addChild:regularBlockMenu];

My program crashes when I tap the menu because it doesn't understand selector call. How do I implement this? I want to create a CommonMethods method because I will use this method over and over in many classes.

Thanks for your help.

Answers


You want to have a method choose in class MainClass? if so, you need modify your createMenu function a bit. Try this,

+ (CCMenu *)createMenu:(NSString *)menuName atPosition:(CGPoint)position forTarget:(id)target 
{
    CCMenuItemImage *menuBlock = [CCMenuItemImage itemWithNormalImage:menuName selectedImage:menuName target:target selector:@selector(choose:)];
    CCMenu *menuBlockMenu = [CCMenu menuWithItems:menuBlock, nil];
    menuBlockMenu.position = position;
    return menuBlockMenu;
}

then in MainClass:

CCMenu *regularBlockMenu = [CommonMethods createMenu:kbRegularBlock atPosition:position forTarget:self];
[self addChild:regularBlockMenu];

and you need to define -(void)choose:(id)sender; in MainClass

So what this is doing is setting the target of choose: to be an instance of MainClass, rather than CommonMethods.


Since self in a class method refers to the class, you should not be making self a target of your selector:

CCMenuItemImage *menuBlock = [CCMenuItemImage itemWithNormalImage:menuName selectedImage:menuName target:self selector:@selector(choose:)];
//                                                                                            HERE ------^^^^

Instead, you should be sending the selector to an object that responds to the selector choose:


Need Your Help

possible to open a mvc view and read lines through code?

c# asp.net-mvc-2

im trying to build a application or a method that goes through all of the views in the project and search for a certain string "EXAMPLE_".. kinda like the visual studio search (through a method, im...