Question

I'm trying to create a subclass object, whose size can be determined when I declare it. For example, do something near to "circle (int width, int height)", and in the C4WorkSpace, attribute two numbers that define the size of the circle. If I understood correctly, you can use initializers for that, like this one:

- (id) initWithNumber: (int) n {
    self = [super init]; ❶ ❷
    if (self) {
        self->_number = n; ❸
    }
    return self; ❹
}

...but I didn't quite understand how to use it and where to put it.

Here is the code I'm working with. I inserted "size" into the parameters of the ellipse, just to illustrate what I'm trying to do. My circle.h file:

#import "C4Shape.h"

@interface circle : C4Shape

@end

And the circle.m one:

#import "circle.h"

@implementation circle

-(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

How is the best way to attribute a variable to a subclass in C4, in this case? If possible, could you explain how I create the object in the C4WorkSpace.m too?

Thanks for your attention. And sorry if I was not clear.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top