You can do this. You have to declare your initializer method into the header file in order for other files to see it. You'll need to create an instance variable called size
and set it to your number. Alternatively, you could use a property. You provide the definition in your Cirlce.m file. I've changed self->size to be just size, since it is an instance variable in your class.
In your C4Workspace.m
you'll need to import the header file, then you'll be able to create one of your objects anywhere in the file. You'll need to call alloc
and then your initWithNumber
in order to create the object. You'll have to call setup
in order to get it to show up on the screen, since that is where you've provided all of your code.
Check out C4: Add panning to an object other than "self" for a related discussion.
Circle.h
#import "C4Shape.h"
@interface Circle : C4Shape
- (id) initWithNumber: (int) n;
@end
Circle.m
#import "Circle.h"
@implementation Circle
{
int size;
}
- (id) initWithNumber: (int) n {
self = [super init];
if (self) {
size = n;
}
return self;
}
-(void) setup
{
[self addGesture:PAN name:@"pan" action:@"move:"];
[self addGesture:TAP name:@"tap" action:@"changeColour"];
[self ellipse:CGRectMake(0, 0, size, size)];
[self setFillColor:[UIColor blackColor]];
[self setStrokeColor:[UIColor blackColor]];
}
-(void) changeColour
{
self.fillColor = [UIColor colorWithRed:[C4Math randomInt: 100]/100.0f green:[C4Math randomInt: 100]/100.0f blue:[C4Math randomInt: 100]/100.0f alpha:1.0f];
}
@end
C4Workspace.m
#import "C4Shape.h"
#import "C4WorkSpace.h"
#import "Circle.h"
@implementation C4WorkSpace
{
Circle * c;
}
-(void)setup
{
c = [[Circle alloc] initWithNumber:100];
[c setup];
[self.canvas addSubview:c];
}
@end