iOS - 无法将UIColor *作为参数传递?

时间:2012-07-10 04:14:14

标签: ios draw uicolor

我想传递UIColor *作为参数,但我的程序在[paramColour set]中不断崩溃。 我知道我可以传入一个字符串并从那里选择UIColor但我只是想知道为什么这不起作用。非常感谢帮助。

- (void) drawRooftopAtTopPointof:(CGPoint)paramTopPoint
                      colour:(UIColor *)paramColour
                    lineJoin:(CGLineJoin)paramLineJoin{

/* Set the color that we want to use to draw the line */
[paramColour set];

/* Get the current graphics context */
CGContextRef currentContext = UIGraphicsGetCurrentContext();

/* Set the line join */
CGContextSetLineJoin(currentContext,
                     paramLineJoin);

/* Set the width for the lines */
CGContextSetLineWidth(currentContext,
                      3.0f);

/* Start the line at this point */
CGContextMoveToPoint(currentContext,
                     paramTopPoint.x - 10,
                     paramTopPoint.y + 8);

/* And end it at this point */
CGContextAddLineToPoint(currentContext,
                        paramTopPoint.x,
                        paramTopPoint.y);

/* Extend the line to another point to
 make the rooftop */
CGContextAddLineToPoint(currentContext,
                        paramTopPoint.x + 10,
                        paramTopPoint.y + 8);

/* Use the context's current color to draw the lines */
CGContextStrokePath(currentContext);

/* Draw the text in the rooftop using a black color */
[[UIColor blackColor] set];

}

这是触发此绘图功能的视图中的drawRect。

- (void)drawRect:(CGRect)rect
{

for (int i=0; i < arrowsToDrawArray.count; i++)
{
    Arrow * arrowObj = [arrowsToDrawArray objectAtIndex:i];
    UIColor * colourOfArrow = [arrowObj colourOfArrow]; // colour determines whether right or wrong
    CGPoint p = [arrowObj arrowPlacement];

//    CGPoint p = [val CGPointValue]; 
    [self drawRooftopAtTopPointof:p colour:colourOfArrow lineJoin:kCGLineJoinMiter];
}
}

这里是Arrow类的定义。

#import <UIKit/UIKit.h>

@class Arrow; 

@interface Arrow : NSObject
{
UIColor * colourOfArrow; 
CGPoint arrowPlacement; 
}
@property (nonatomic,retain) UIColor * colourOfArrow;
@property (nonatomic) CGPoint arrowPlacement; 

@end

1 个答案:

答案 0 :(得分:0)

嗨,谢谢你的帮助,

我决定用构造函数正确地完成它,所有这些都是新代码。似乎现在正在工作。

arrow.h

#import <UIKit/UIKit.h>

@class Arrow; 

@interface Arrow : NSObject
{
UIColor * colourOfArrow; 
CGPoint arrowPlacement; 
}
@property (nonatomic,strong) UIColor * colourOfArrow;
@property (nonatomic) CGPoint arrowPlacement; 

+ (Arrow *) createArrowWithColour :(UIColor *)paramColour andPosition :(CGPoint)      xPosition;

@end   

arrow.m

#import "Arrow.h"

@implementation Arrow
@synthesize colourOfArrow;
@synthesize arrowPlacement;

+ (Arrow *) createArrowWithColour :(UIColor *)paramColour andPosition :(CGPoint) xPosition
{
Arrow * tempArrow = [[Arrow alloc] init];
[tempArrow setColourOfArrow:paramColour];
[tempArrow setArrowPlacement:xPosition];

return tempArrow;
}

@end