Question

I have class "Button," that is a subclass of CCNode. Since a button (as in computer buttons), IS-NOT a CCLabelBMFont or CCSprite, I subclassed it as a CCNode. In addition, I have extended CCLabelBMFont with child "CCLabelCustom" that has some fancy logic of its own. With that, I have moved CCLabelCustom *buttonLabel and CCSprite *buttonSprite as an iVar of "Button." I want "Button" to be as abstract as possible, with no default buttonLabel or buttonSprite. The thing is, how do I go initing both CCLabelCustom and CCSprite? The CCLabelCustom have some long inits with more than four parameter and additional properties can be assigned.

// Rough Idea to help elaborate my classes
@interface Button : CCNode

@property (nonatomic, strong) CCLabelCustom *buttonLabel;
@property (nonatomic, strong) CCSprite *buttonSprite;

@end


// Rough Idea to help elaborate my classes
@interface CCLabelCustom : CCLabelBMFont

@property (nonatomic, strong) SomeProperty *someVar;
@property (nonatomic, strong) AnotherProperty *anotherVar;

-(void)fancyMethod;
-(void)initWithSomething:(Something*)something andAnother:(Another*)another alongWith:(Fun*)fun;

@end

Is there any architecture that I don't know of? Is there any method that I can init "Button" with the two iVar from outside the class? I don't want any default value that I can define inside the "Button" class. I want to do it from a outside layer. Thank you in advance.

Was it helpful?

Solution

Put the method initWithSomething:(Something*)something andAnother:(Another*)another alongWith:(Fun*)fun; on your Button class. And on this init of your Button, also init the CCLabelCustom with these parameters.

Then when you will init your button outside the class, use this method and pass the parameters.

PS.: The method initWithSomething must return an ID object: -(id)initWithSomething:

Example:

// Outside class
Button *button = [[Button alloc] initWithSomething:something andAnother:another alongWith:fun];

...
// Your Button custom init
- (id)initWithSomething:(Something*)something andAnother:(Another*)another alongWith:(Fun*)fun
{
    self = [self init];
    if (self) {
        self.buttonLabel = [[CCLabelCustom alloc] initWithSomething:something andAnother:another alongWith:fun]
    }
    return self;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top