ios - 将值传递给 UIView 返回 nil

标签 ios uiview uiviewcontroller

我有一个 UIViewController GamePlayViewController 和一个 UIView JoystickView

我正在尝试为 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;
}

关于ios - 将值传递给 UIView 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975082/

相关文章:

ios - 平稳加速进度View

ios - ULabel 的自动布局问题

ios - UIView drawViewHierarchyInRect 给出像素化结果?

ios - 如何在两个 View Controller 之间共享mapkit的变量?

iphone - 尽管有任何设备旋转,layoutsubviews 只调用一次

ios - iOS 7 中 UITableView 的半透明效果

ios - 将数据传递给下一个 ViewController 时出错

iphone - 从 CGPDfDocument 页面保存图像并不完全适合 UIImageview

iphone - 在 iOS 上将中文 char* 转换为 NSString?

ios - 返回IOS Root View