简体   繁体   English

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

[英]Displaying an UIImageView property from another class programmatically

Well, here's the situation: I've got... 好吧,这是情况:我有...

  • a custom class with an UIImageView -property, let's call it the Enemy -class 一个带有UIImageView -property的自定义类,我们称它为Enemy -class
  • a ViewController 一个ViewController
  • a NSMutableArray to help create multiple instances of Enemy (called enemies ) 一个NSMutableArray来帮助创建多个Enemy实例(称为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 ) 能够通过我的ViewController的方法(例如[self spawnEnemy]; ,其中selfViewController )创建无限数量的Enemy -Instances
  • and, subsequently, display the UIImageView property (let's call it " enemyImage ") on the view that is controlled by my ViewController 然后,在由我的ViewController控制的view上显示UIImageView属性(将其称为“ enemyImage ”)

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 : 这运行没有错误, randomLocation被调用(与NSLog尝试),并且如果我在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? 但是,仍然没有enemieView出现在屏幕上...我在做什么错?

Thank you so much for your help and time. 非常感谢您的帮助和时间。

==== Edit ==== ====编辑====

Here's the relevant code from Enemy.h/Enemy.m: 这是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. 我还更正了spawnEnemy的最后一行,以正确发送createEnemy。

You don't include the code in Enemy where you alloc/init the UIImageView property. 您没有在Enemy中分配/初始化UIImageView属性的代码。 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. 除非此代码明确指定了具有所需sizeoriginCGRect ,否则将使用CGRectZero初始化视图,这意味着即使您正确添加了子视图(看起来像您一样),您仍然看不到它任何地方。

Post the Enemy code, and the problem will probably be immediately apparent. 发布Enemy代码,问题可能会立即显现。

Have you called -createEnemy before you added them to your view? 在将它们添加到视图之前,您是否曾致电-createEnemy?

OK, you've got this checked. 好的,您已经检查了这一点。

Then maybe you should check as @MusiGenesis suggested. 然后,也许您应该按照@MusiGenesis的建议进行检查。

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: 通过以下方式打印其frame.size:

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

  2. set a breakpoint where you feel great, and check with your debugger: 在感觉良好的地方设置一个断点,并与调试器联系:

    p (CGRect)[[enemy enemyImage] frame] p(CGRect)[[敌人敌我图片]框架]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM