我有一个UIViewController GamePlayViewController和一个UIView JoystickView. 我试图设置一个浮动值到JoystickView. 在GamePlayViewController.m中 JoystickView *joystick = [[JoystickView alloc] initWithFrame:CGRectMake(0, 0, 100
我试图设置一个浮动值到JoystickView.
在GamePlayViewController.m中
JoystickView *joystick = [[JoystickView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; joystick.velocity = 123.0f; [self.view addSubview:joystick];
在JoystickView.h中
@property(nonatomic) float velocity;
.M
@synthesize velocity; -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSLog(@"%f", velocity); //output is 0 } return self; }
我错过了什么?
您可以通过创建initWithFrameAndVelocity方法来覆盖initWithFrame-(id)initWithFrame:(CGRect)frame andVelocity:(float)v { self = [super initWithFrame:frame]; if (self) { velocity = v; NSLog(@"%f", velocity); //output is 0 } return self; }