开发者

Passing Object value to UIView

开发者 https://www.devze.com 2023-01-03 11:06 出处:网络
See the code here @interface StaticView : UIView { Properties *prop; } @property (retain) Properties *prop;

See the code here

@interface StaticView : UIView {
    Properties *prop;
}
@property (retain) Properties *prop;
@end

and i am attaching this view via code

[super viewDidLoad];
StaticView *sView = [[StaticView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
sView.prop.glowIntensity = 0.85f;

[self.view addSubview:sView];

but in my drawRect Method of StaticView i am always getting prop.glowIntensity = 0.0000

- (void)drawRect:(CGRect)rect {
        NSLog(@"%f", prop.glowIntensity);
} 

Here is my 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

here is implementation Properties.m

#import "Properties.h"

@implementation Properties

@synthesize b开发者_如何学PythongColor;
@synthesize foreColor;
@synthesize glowIntensity;

-(void) initbgColor:(UIColor *)bgC foreColor:(UIColor *)fC glowIntensity:(float) gI
{
    self.bgColor = bgC;
    self.foreColor = fC;
    self.glowIntensity = gI;

}
@end

How can i pass right value to my StaticView class?


Try changing your code to this:

@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;
    }
}

Edit: I think Vladimir is right, too.


It looks you do not create or set your view's prop variable before using it - by default it equals nil and calling glowIntensity on nil object just returns 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消