我有一個小小的UIView對象CircleColorView.m,它簡單地創建一個帶有彩色圓圈的視圖。然後,我使用該視圖作爲一組按鈕(所有不同顏色)的背景。將UIColor作爲參數傳遞時崩潰
我的問題發生在drawRect:方法被調用時。我崩潰了,但是有時候,當我引用UIColor的對象顏色時。
我很困惑。這是我的UIView:
ColorCircleView.h
#import <UIKit/UIKit.h>
#import "Constants.h"
@interface CircleColorView : UIView {
UIColor *color;
}
@property (nonatomic, retain) UIColor *color;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)circleColor;
@end
這裏是ColorCircleView.m
#import "CircleColorView.h"
@implementation CircleColorView
@synthesize color;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)circleColor {
if ((self = [super initWithFrame:frame])) {
color = [UIColor colorWithCGColor:[circleColor CGColor]];
// have also tried
// color = circleColor;
}
return self;
}
- (void) drawRect: (CGRect) aRect
{
CGFloat iconSize = self.frame.size.width;
// Create a new path
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
// Set up fill for circle
const CGFloat* fill = CGColorGetComponents(color.CGColor);
CGContextSetFillColor(context, fill);
// Add circle to path
CGRect limits = CGRectMake(8.0f, 8.0f, iconSize - 16.0f, iconSize - 16.0f);
CGPathAddEllipseInRect(path, NULL, limits);
CGContextAddPath(context, path);
CGContextFillEllipseInRect(context, limits);
CGContextFillPath(context);
CFRelease(path);
}
- (void)dealloc {
[color release];
[super dealloc];
}
@end
這裏是我用來創建和CircleColorView添加到按鈕圖像的代碼。它是在一個循環中,通過一串顏色值由a分隔的字符串;
NSArray *values = [[NSArray alloc] initWithArray:[[[colorListArray objectAtIndex:i] objectAtIndex:1] componentsSeparatedByString:@";"]];
float red = [[values objectAtIndex:0] floatValue];
float green = [[values objectAtIndex:1] floatValue];
float blue = [[values objectAtIndex:2] floatValue];
UIColor *color = [[UIColor alloc]
initWithRed: (float) (red/255.0f)
green: (float) (green/255.0f)
blue: (float) (blue/255.0f)
alpha: 1.0];
UIButton *newColorButton = [UIButton buttonWithType:0];
//Create Colored Circle
CircleColorView *circle = [[CircleColorView alloc] initWithFrame:CGRectMake(0, 0, 75, 75) andColor:color ];
circle.backgroundColor = [UIColor clearColor];
//Set Button Attributes
[newColorButton setTitle:[[colorListArray objectAtIndex:i] objectAtIndex:1] forState:UIControlStateDisabled];
[newColorButton setFrame:CGRectMake(600+(i*82), 12, 75, 75)]; //set location of each button in scrollview
[newColorButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
[newColorButton setTag:tagNum];
[barContentView addSubview:newColorButton];
[circle release];
[color release];
[values release];
我記錄下來看看會發生什麼。看起來它運行CircleColorView的initWithFrame:andColor:很好。然後,當drawRect:被調用時,它會在第一次引用屬性'color'時崩潰。即使它只是要求像[顏色描述]這樣的描述。
任何想法。我是否創建了這個UIColor *顏色錯誤?還是保留它錯了?這是另一件奇怪的事情。這段代碼運行得很好。然後,當我退出並重新啓動應用程序時,它會崩潰。爲了讓它再次運行,我已經刪除了iPhone模擬器中的構建文件和應用程序文件夾。這將允許它再次工作。唯一能夠持續工作的其他方法是將UIColor * color @property更改爲分配,反之亦然。這將允許我重新構建應用程序,並且不會再出現一次或兩次問題。然後它崩潰。哦,它在設備上也做同樣的事情。有任何想法嗎???
由於提前,
馬克
貴的問題得到了解決?請將答案標記爲已接受的答案。 – JoePasq 2010-07-06 15:39:38