I created a method in ClassA and want to call it in ClassB.m.
In ClassA.h I have this:
@interface ClassA : NSObject <NSCoding>
...
+ (NSInteger) methodA:(CGPoint)touchPoint;
...
@end
And in ClassA.m I have declared methodA:
+ (NSInteger)methodA:(CGPoint)touchPoint
{
// return an integer based on touchPoint's value
}
And in ClassB.m:
#import "ClassA.h"
...
-(void)methodThatCallsMethodA
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
NSInteger integerUsingClassA = [ClassA methodA:touchPoint];
// do some stuff
}
I have a feeling that the issue lies in how I am calling the method and that the object, which is right now ClassA, is wrong, but I’m not sure. The error is unrecognized selector sent to class .... Keep in mind that if I create a method identical to methodA within ClassB.m I am able to call it like I am calling in methodThatCallsMethodA on the object self with no problem.
~~~~~~~~~~~
I have also tried this in ClassB.h:
#import "ClassA.h"
@interface...
@property(nonatomic, retain)ClassA *objectOfClassA;
...
@end
And changed ClassB.m:
#import "ClassA.h"
@synthesize objectOfClassA;
- (void)methodThatCallsMethodA
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if(!self.objectOfClassA)
self.objectOfClassA = [[ClassA alloc] init];
NSInteger integerUsingClassA = [self.objectOfClassA methodA:touchPoint];
NSLog(@"ClassA: %i", integerUsingClassA);
}
But now it’s warning that instance method -methodA not found.
The first part is correct for calling a class method part of
ClassAinClassB(marked by+(NSInteger)inClassA). The second part is correct for calling an instance method part ofClassAinClassB(marked by-(NSInteger)inClassA).The error was the fact that Xcode 4 wasn’t saving changes and was building using older versions.