I am in ViewController, trying to access a method in object “cat” owned by object “backgroundpicture”. ViewController has an instance of backgroundpicture.
The method/message in “cat.h”:
@interface Cat : NSObject
-(BOOL)checkIfTouchHit:(float) xx :(float) yy;
@end
“Cat.m”:
- (BOOL)checkIfTouchHit:(float) xx :(float) yy{
NSLog(@"Inside checkIfTouchHit");
return YES;
}
“BackGroundPicture.h”:
#import "Cat.h"
@interface BackGroundPicture : NSObject
@property (strong) Cat * katt;
@end
“BackGroundPicture.m”:
@implementation BackGroundPicture
@synthesize katt = _katt
@end
“ViewController.m”:
@interface ViewController ()
@property (strong) BackGroundPicture * bakgrunnsbilde;
@end
@implementation BackGroundPicture
@synthesize bakgrunnsbilde = _bakgrunnsbilde;
- (void)viewDidLoad
{...
[[self.bakgrunnsbilde katt] checkIfTouchHit :(float)touchLocation.x :(float)touchLocation.y]
...}
The string inside the method “checkIfInside” in cat will not show up. I also tried
[_bakgrunnsbilde katt]…
but with the same lack of result, and I believe this is compiled the same way. I am wondering what I am missing here, and hope someone can help. Thanks 🙂
edit I forgot to add a few lines from my BackGroundPicture.m. It is a method run on start from the ViewDidLoad in ViewController. It is like this in BackGroundPicture.m:
- (void)createObjects {
Cat * katt = [[Cat alloc] init];
}
it is called from ViewController.m like so:
- (void)viewDidLoad
{
[_bakgrunnsbilde createObjects];
}
I know that this get executed. I hope this edit makes sense, my head is ruined after a long day 🙂 Going to check back tomorrow morning.
It will work, but BackGroundPicture.m needs to allocate a cat first.
So in BackGroundPicture.m, do this:
In general, remember to allocate objects before you use them. You may also need to create a BackGroundPicture, too as Valentin points out. In viewDidLoad, do this: