0

我试图从Class A例如位于的方法调用绘图方法Class B,正在调用该方法但没有绘图发生。

- (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]];
}

为什么我可以从其他类调用这个方法?

4

2 回答 2

1

首先创建类' YourView',它是 UIView 的子类。编写viewDidLoadB类中的分配代码方法

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

- (void)drawRect:(CGRect)rect在 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]];
}

我想这会对你有所帮助。

于 2012-05-29T11:43:41.927 回答
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.

于 2012-05-29T11:31:32.360 回答