jkigel
jkigel

Reputation: 1592

iPhone Quartz - Call Drawing Method From Other Class

I'm trying to call a drawing method from Class A for example, the method located in Class B, the method is being called but no drawing happen.

- (void)drawIt
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);

    NSString *string = [NSString stringWithString:@"TEXT"];
    [string drawAtPoint:CGPointMake(12, 51) withFont:[UIFont fontWithName:@"Helvetica" size:35.0f]];
}

Why can I call this method from other class?

Upvotes: 0

Views: 179

Answers (2)

Prasad G
Prasad G

Reputation: 6718

First create class 'YourView' which is subclass of UIView. Write allocation code viewDidLoad method which is in Class B

- (void)viewDidLoad{
 YourView *temp = [[YourView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:temp];
}

Implement - (void)drawRect:(CGRect)rect method in YourView.m

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    NSString *string = [NSString stringWithString:@"TEXT"];
    [string drawAtPoint:CGPointMake(12, 51) withFont:[UIFont fontWithName:@"Helvetica" size:35.0f]];
}

I think it will be helpful to you.

Upvotes: 1

Danilo Gomes
Danilo Gomes

Reputation: 757

If you are using a UIView or some subclass you need to overload the drawRect method. So, inside drawRect you call your method in other class. Also, you can pass your context via parameter too.

Upvotes: 0

Related Questions