以编程方式显示另一个类的 UIImageView 属性

发布于 2024-12-13 03:45:11 字数 1870 浏览 1 评论 0原文

嗯,情况是这样的: 我有...

  • 一个带有 UIImageView 属性的自定义类,我们将其称为 Enemy
  • a ViewController
  • a NSMutableArray 帮助创建 Enemy 的多个实例(称为 enemies

以及我想要的:

  • 能够创建无限数量的 Enemy >-通过方法实例化我的 ViewController (如 [self spawnEnemy];,其中 selfViewController
  • ,随后显示由我的 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 the Enemy-class
  • a ViewController
  • a NSMutableArray to help create multiple instances of Enemy (called enemies)

and what I want:

  • be able to create an unlimited amount of Enemy-Instances through a method in my ViewController (like [self spawnEnemy];, where self is the ViewController)
  • and, subsequently, display the UIImageView property (let's call it "enemyImage") on the view that is controlled by my ViewController

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 enemieViews 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各空 2024-12-20 03:45:11

您不将代码包含在分配/初始化 UIImageView 属性的 Enemy 中。除非此代码显式指定具有所需的 sizeoriginCGRect,否则视图将使用 CGRectZero 进行初始化>,这意味着即使您正确添加了子视图(看起来像您是正确的),您仍然不会在任何地方看到它。

发布 Enemy 代码,问题可能会立即显现出来。

You don't include the code in Enemy where you alloc/init the UIImageView property. Unless this code explicitly specifies a CGRect with the size and origin that you want, the view will be initialized with CGRectZero, 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.

静若繁花 2024-12-20 03:45:11

在将它们添加到视图之前,您是否调用过 -createEnemy?

好的,您已经选中了此项。

那么也许你应该按照@MusiGenesis 的建议进行检查。

为此,您需要检查敌人图像的属性。

您可以通过以下任一方式执行此操作:

  1. 打印其frame.size:

    NSLog(@"enemyImage 帧大小:%@", NSStringFromCGRect(enemy.enemyImage));

  2. 在您感觉良好的地方设置一个断点,并使用调试器检查:

    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:

  1. print its frame.size by:

    NSLog(@"enemyImage frame size: %@", NSStringFromCGRect(enemy.enemyImage));

  2. set a breakpoint where you feel great, and check with your debugger:

    p (CGRect)[[enemy enemyImage] frame]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文