Well, here’s the situation:
I’ve got…
- a custom class with an
UIImageView-property, let’s call it theEnemy-class - a
ViewController - a
NSMutableArrayto 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];, whereselfis theViewController) - and, subsequently, display the
UIImageViewproperty (let’s call it “enemyImage“) on theviewthat 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 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.
You don’t include the code in
Enemywhere you alloc/init theUIImageViewproperty. Unless this code explicitly specifies aCGRectwith thesizeandoriginthat 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
Enemycode, and the problem will probably be immediately apparent.