0

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?

STW
  • 44,917
  • 17
  • 105
  • 161
jkigel
  • 1,592
  • 6
  • 28
  • 49

2 Answers2

1

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.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • Thanks but I don't want to make this draw straight after init... I also want to be able to draw and erase drawing manually – jkigel May 29 '12 at 11:48
0

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.

Danilo Gomes
  • 767
  • 5
  • 15
  • Thank you, the calls is superclass is UITableView and I'm using to drawRect but I don't want to make the draw on the object init. – jkigel May 29 '12 at 11:44