でメソッドを作成し、でClassA呼び出したいClassB.m。
私はこれClassA.hを持っています:
@interface ClassA : NSObject <NSCoding>
...
+ (NSInteger) methodA:(CGPoint)touchPoint;
...
@end
そしてClassA.m私は宣言しましたmethodA:
+ (NSInteger)methodA:(CGPoint)touchPoint
{
// return an integer based on touchPoint's value
}
そしてでClassB.m:
#import "ClassA.h"
...
-(void)methodThatCallsMethodA
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
NSInteger integerUsingClassA = [ClassA methodA:touchPoint];
// do some stuff
}
問題はメソッドの呼び出し方法にあり、現在のオブジェクトClassAが間違っているように感じますが、よくわかりません。エラーはunrecognized selector sent to class ...です。methodA内と同じメソッドを作成すると、オブジェクトClassB.mを呼び出しているように問題なく呼び出すことができることに注意してください。methodThatCallsMethodAself
~~~~~~~~~~~
私もこれを試しましたClassB.h:
#import "ClassA.h"
@interface...
@property(nonatomic, retain)ClassA *objectOfClassA;
...
@end
そして変更されました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);
}
しかし今、それは警告ですinstance method -methodA not found。