以编程方式显示另一个类的 UIImageView 属性
嗯,情况是这样的: 我有...
- 一个带有
UIImageView
属性的自定义类,我们将其称为Enemy
类 - a
ViewController
- a
NSMutableArray
帮助创建Enemy
的多个实例(称为enemies
)
以及我想要的:
- 能够创建无限数量的
Enemy
>-通过方法实例化我的ViewController
(如[self spawnEnemy];
,其中self
是ViewController
) - ,随后显示由我的
ViewController
控制的view
上的UIImageView
属性(我们称之为“enemyImage
”)
我已经尝试过这样的事情:
-(Enemy *) spawnEnemy
{
Enemy *tempEnemy = [Enemy new];
[enemies addObject:(Enemy*)tempEnemy];
[self.view addSubview:(UIImageView*)[[enemies objectAtIndex:[enemies count]] enemyImage]];
//randomLocation is declared in the Enemy-Class and just assigns a random
//CGPoint to self.enemyImage.center
[[enemies objectAtIndex:[enemies count]] randomLocation];
return [[enemies objectAtIndex:[enemies count]]createEnemy];
}
运行没有错误, randomLocation 被调用(使用 NSLog 尝试),并且如果我在 ViewController 的另一个方法中执行类似的操作:
[[self spawnEnemy] enemyTestMethod];
enemyTestMethod 也会被执行。
但是,屏幕上仍然没有显示 enemieView
... 我做错了什么?
非常感谢您的帮助和时间。
==== 编辑 ====
这是来自 Enemy.h/Enemy.m 的相关代码:
@interface Enemy : NSObject
{
UIImageView *enemyImage;
}
@property (nonatomic, retain) IBOutlet UIImageView *enemyImage;
-(Enemy*) createEnemy;
//Enemy.m
@implementation Enemy
@synthesize enemyImage, speed;
-(Enemy *) createEnemy
{
self.enemyImage = [[UIImageView alloc] init];
[self.enemyImage setImage:[UIImage imageNamed:@"enemy.png"]];
return self;
}
我还更正了 spawnEnemy
方法中的最后一行,以正确发送 createEnemy。
Well, here's the situation:
I've got...
- a custom class with an
UIImageView
-property, let's call it theEnemy
-class - a
ViewController
- a
NSMutableArray
to help create multiple instances ofEnemy
(calledenemies
)
and what I want:
- be able to create an unlimited amount of
Enemy
-Instances through a method in myViewController
(like[self spawnEnemy];
, whereself
is theViewController
) - and, subsequently, display the
UIImageView
property (let's call it "enemyImage
") on theview
that is controlled by myViewController
I've tried something like this:
-(Enemy *) spawnEnemy
{
Enemy *tempEnemy = [Enemy new];
[enemies addObject:(Enemy*)tempEnemy];
[self.view addSubview:(UIImageView*)[[enemies objectAtIndex:[enemies count]] enemyImage]];
//randomLocation is declared in the Enemy-Class and just assigns a random
//CGPoint to self.enemyImage.center
[[enemies objectAtIndex:[enemies count]] randomLocation];
return [[enemies objectAtIndex:[enemies count]]createEnemy];
}
This runs without errors, randomLocation
gets called (tried with NSLog), AND if I do something like this in another Method of ViewController
:
[[self spawnEnemy] enemyTestMethod];
enemyTestMethod is being executed as well.
But still, no enemieView
s are displayed on the screen...
What am I doing wrong?
Thank you so much for your help and time.
==== Edit ====
Here's the relevant code from Enemy.h/Enemy.m:
@interface Enemy : NSObject
{
UIImageView *enemyImage;
}
@property (nonatomic, retain) IBOutlet UIImageView *enemyImage;
-(Enemy*) createEnemy;
//Enemy.m
@implementation Enemy
@synthesize enemyImage, speed;
-(Enemy *) createEnemy
{
self.enemyImage = [[UIImageView alloc] init];
[self.enemyImage setImage:[UIImage imageNamed:@"enemy.png"]];
return self;
}
I also corrected the last line in the spawnEnemy
-Method to properly send createEnemy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不将代码包含在分配/初始化
UIImageView
属性的Enemy
中。除非此代码显式指定具有所需的size
和origin
的CGRect
,否则视图将使用CGRectZero
进行初始化>,这意味着即使您正确添加了子视图(看起来像您是正确的),您仍然不会在任何地方看到它。发布
Enemy
代码,问题可能会立即显现出来。You don't include the code in
Enemy
where you alloc/init theUIImageView
property. Unless this code explicitly specifies aCGRect
with thesize
andorigin
that you want, the view will be initialized withCGRectZero
, which means even if you're correctly adding the subview (and it looks like you are) you still won't see it anywhere.Post the
Enemy
code, and the problem will probably be immediately apparent.在将它们添加到视图之前,您是否调用过 -createEnemy?好的,您已经选中了此项。
那么也许你应该按照@MusiGenesis 的建议进行检查。
为此,您需要检查敌人图像的属性。
您可以通过以下任一方式执行此操作:
打印其frame.size:
NSLog(@"enemyImage 帧大小:%@", NSStringFromCGRect(enemy.enemyImage));
在您感觉良好的地方设置一个断点,并使用调试器检查:
p (CGRect)[[敌人敌人图像] 帧]
Have you called -createEnemy before you added them to your view?OK, you've got this checked.
Then maybe you should check as @MusiGenesis suggested.
To do this, you need to inspect the properties of your enemyImage.
You can do this in either of the following ways:
print its frame.size by:
NSLog(@"enemyImage frame size: %@", NSStringFromCGRect(enemy.enemyImage));
set a breakpoint where you feel great, and check with your debugger:
p (CGRect)[[enemy enemyImage] frame]