0

I'm wondering how I can access self from a class method.

In this instance method I have no problem accessing self:

- (void) ccTouchesEnded:(NSSet *) touches withEvent:(UIEvent *) event{

    UITouch *theTouch = [touches anyObject];
    if (theTouch.tapCount == 2) {

        // self is available here to change background but I need to call it from 
        // a class method since it's being invoked elsewhere.

        [TouchTrailLayer testCall];
    }
}

+ (void) testCall {
    [TouchTrailLayer changeBackground];
}

How do can I refer to self in the class method below, as if it were an instance method? Or, how do you call an instance method using a class method (pick the best)?

+ (void) changeBackground {

    // this is where self doesn't work

    [self removeChildByTag:100 cleanup:YES];
    CGSize size = [[CCDirector sharedDirector] winSize];
    CCSprite *bg = [CCSprite spriteWithFile:@"Default-hd.png"];
    bg.position = ccp( size.width /2 , size.height/2 );
    bg.tag = 100;
    [self addChild:bg z:0];
}
yuji
  • 16,695
  • 4
  • 63
  • 64
chad
  • 27
  • 1
  • 8
  • your answer to the first option probably is http://stackoverflow.com/questions/2121880/call-instance-method-from-class-method – Bourne Sep 16 '11 at 03:23
  • 1
    Hence the suggestion would be to rethink your design/flow. – Bourne Sep 16 '11 at 03:25
  • very helpful in understanding what's going on thanks – chad Sep 16 '11 at 03:33
  • The whole idea behind `self` is that it's a reference to a class *instance*. In a class method there is no instance. You should change the method signature to `- (void) changeBackground` if you want to access an instance of the class during it. –  Sep 16 '11 at 03:37
  • Changing the method signature to `- (void) changeBackground` actually works beautifully, except where AppDeligate needs to call `applicationWillEnterForeground`. If I could just access it from the AppDeligate it would be perfect. – chad Sep 16 '11 at 03:51
  • @chad - You can have your AppDelegate subclass store a pointer to the instance it needs to call. It's not uncommon to have a dozen or more other objects "anchored" in the AppDelegate, and, in fact, this is another way to essentially accomplish the "singleton" function. – Hot Licks Sep 16 '11 at 11:59
  • @Daniel How do you store a pointer to the instance in AppDelegate? – chad Sep 16 '11 at 21:35
  • Create your subclass of UIApplicationDelegate, declare a field in it to hold your pointer, make it a property or otherwise provide getter/setter functions, then set the pointer. No different from setting a pointer in any other object. – Hot Licks Sep 16 '11 at 22:27

2 Answers2

2

You can't, generally, get "self" in a static/class method because there may be no instances of the class at all, or there may be several.

However, if you know that there can only ever be one instance of the class you can implement something along the lines of a "singleton".

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • singleton did the trick. [Singleton link](http://iphone.galloway.me.uk/iphone-sdktutorials/singleton-classes/) – chad Sep 16 '11 at 04:31
0

Singleton is very handy and can serve most of the purposes of what class methods attempt to achieve. You can, however, call self within a class method to call another class method of the same class. Another minor point is that in your instance method you can use [self class] to call your class method, instead of spelling out the class name. It's more portable and elegant imo.

Hampden123
  • 1,248
  • 1
  • 14
  • 16