将 UIColor 作为参数传递时崩溃 [英] Crashing while passing UIColor as argument

查看:18
本文介绍了将 UIColor 作为参数传递时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的 UIView 对象 CircleColorView.m,它只是创建一个带有彩色圆圈的视图.然后我使用该视图作为一堆按钮(所有不同颜色)的背景.

I have a little UIView object, CircleColorView.m, that simply creates a view with a colored circle in it. I then use that view as the background to a bunch of buttons (all different colors).

我的问题发生在 drawRect: 方法被调用时.我崩溃了,但只是有时,当我引用 UIColor 对象颜色时.

My problem happens when the drawRect: method gets called. I crash, but only sometimes, when I reference the object color which is a UIColor.

我很困惑.这是我的 UIView:

I am very confused. Here is my UIView:

ColorCircleView.h

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

And here is 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 并将其添加到按钮图像的代码.它位于一个循环中,该循环遍历一个字符串数组,其中的颜色值由 ;

Here is the code that I use to create and add the CircleColorView to a button image. It is inside a loop that is going through an array of strings with color values separated by 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' 时崩溃.即使它只是要求它提供 [color description] 之类的描述.

I've logged it to see what happens. Looks like it runs CircleColorView's initWithFrame:andColor: just fine. Then when the drawRect: is called, it will crash the first time the property 'color' is referenced. Even if it is just asking it for a discription like [color description].

任何想法.我创建这个 UIColor *color 错了吗?还是保留错误?这是另一个奇怪的事情.这段代码运行了一段时间就好了.然后当我退出并重新启动应用程序时,它会崩溃.为了让它再次工作,我删除了 iPhone 模拟器中的构建文件和 app 文件夹.这将允许它再次工作.唯一能让它持续工作的另一件事是只需将 UIColor *color @property 更改为assign,反之亦然.这将允许我重新构建应用程序并再次运行它,没有任何问题.然后它崩溃了.哦,它在设备上做同样的事情.有任何想法吗???

Any ideas. Am I creating this UIColor *color wrong? Or retaining it wrong? Here is another strange thing. This code runs just fine for a while. Then when I quit and restart the app it will crash. To get it to work again I have deleted the build file and the app folder in the iPhone simulator. That will allow it to work again. The only other thing that will consistantly get it to work is by just changing the UIColor *color @property to assign or vice versa. That will allow me to rebuild the app and run it with no problems once or twice again. Then it crashes. Oh, and it does the same thing on the device. Any ideas???

提前致谢,

标记

推荐答案

这一行声明了颜色属性,分配时将保留颜色.

This line declares the color property, which will retain the color when assigned.

@property (nonatomic, retain) UIColor *color;

这一行绕过了属性设置器,直接为成员分配了一个自动释放的对象.

This line bypasses the property setter and assigns an autoreleased object directly to the member.

color = [UIColor colorWith...

您可以通过以下任一方式修复它:

You can fix it with either:

self.color = [UIColor colorWith...

[color retain];

color = [[UIColor alloc] initWith...

这篇关于将 UIColor 作为参数传递时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆