The variables bounds, width and height are at present local variables. I cannot access them from other classes or even access them from another method.
How can I make these variables available to the whole instance? I have tried placing them within the .h file and renaming them to CGFloats to no avail.
#import "TicTacToeBoard.h"
@implementation TicTacToeBoard
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
float width = bounds.size.width;
float height = bounds.size.height;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.3, 0.3, 0.3, 1);
CGContextSetLineWidth(ctx, 5);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextMoveToPoint(ctx, width/3, height * 0.95);
CGContextAddLineToPoint(ctx, width/3, height * 0.05);
CGContextStrokePath(ctx);
}
@end
You can use properties to make variables accessible to other objects.
In your interface add something like this:
and then add
to your implementation.
Two methods will be created to get the property and to change it.
You can change the value of a property within a class using
[self setMystring:@"new value"]or if you have the same variable already declared in the interface and then create a property from it you can keep using your variables within the class the way you are .There’s more info on properties in the developer docs: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1