开发者

Calling Objective C Instance Method from Another Class

开发者 https://www.devze.com 2023-01-19 09:36 出处:网络
I have class called \"HelloWorld.m\" and another class called \"Person.m\". I have declared Person instance in the HelloWorld.m class as follows:

I have class called "HelloWorld.m" and another class called "Person.m". I have declared Person instance in the HelloWorld.m class as follows:

Person *person;

Now, when the user touches the screen I fire the following method:

[person foo]; 

But I get the error saying invalid selector sent.

NOTE: Person class inherits from the CCSprite class. What am I doing wrong?

Person.h:

-(void) foo; 

Person.m:

-(void) foo 
{
    NSLog(@"foo called!");
}

UPDATE 1: In the HelloWorld.h and HelloWorld.m I have declarated person as a property. Here is the call to the person.foo method.

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

    [self.person foo]; 
}

HelloWorld.h:

开发者_运维技巧
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "Person.h" 

// Importing Chipmunk headers
#import "chipmunk.h"

// HelloWorld Layer
@interface HelloWorld : CCLayer
{
    cpSpace *space;
    Person *person; 

}

// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
-(void) step: (ccTime) dt;
-(void) addNewSpriteX:(float)x y:(float)y;

@property (nonatomic,retain) Person *person;


@end


Did you import the Person header into the top of the HelloWorld file?

#import "Person.h"

Did you initialise the person pointer?

person = [[Person alloc] init];

Does the Person class have a foo method?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号