请参阅此处的代码
@interface StaticView : UIView {
Properties *prop;
}
@property (retain) Properties *prop;
@end
我正在通过代码附加此视图
[super viewDidLoad];
StaticView *sView = [[StaticView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
sView.prop.glowIntensity = 0.85f;
[self.view addSubview:sView];
但是在我的staticView的drawRect方法中,我总是得到prop.glowIntensity = 0.0000
- (void)drawRect:(CGRect)rect {
NSLog(@"%f", prop.glowIntensity);
}
这是我的Properties.h
@interface Properties : NSObject {
UIColor *bgColor;
UIColor *foreColor;
float glowIntensity;
}
@property(retain) UIColor *bgColor;
@property(retain) UIColor *foreColor;
@property float glowIntensity;
-(void) initbgColor:(UIColor *)bgC foreColor:(UIColor *)fC glowIntensity:(float) gI;
@end
这里是实现Properties.m
#import "Properties.h"
@implementation Properties
@synthesize bgColor;
@synthesize foreColor;
@synthesize glowIntensity;
-(void) initbgColor:(UIColor *)bgC foreColor:(UIColor *)fC glowIntensity:(float) gI
{
self.bgColor = bgC;
self.foreColor = fC;
self.glowIntensity = gI;
}
@end
如何将正确的值传递给我的StaticView类?
答案 0 :(得分:3)
尝试将代码更改为:
@property (assign) float glowIntensity; // We don't want to retain the float like done by default (at least I think it's default)
- (void)initWithBgColor:(UIColor *)bgC foreColor:(UIColor *)fC glowIntensity:(float)gI {
if (self = [super init]) { // We need to init self
bgColor = bgC; // Don't use the accessors in -init
foreColor = fC;
glowIntensity = gI;
}
}
编辑:我认为弗拉基米尔也是对的。
答案 1 :(得分:2)
在使用它之前看起来你没有创建或设置你的视图的prop变量 - 默认情况下它等于nil并且在nil对象上调用glowIntensity
只返回0。