开发者

Adding a UIImageView to main view from a struct

I'm new to Obj-C and I'm looking for a little help.

I have a struct which I defined in my header file.:

struct EnemyBall {

    int ballnum;
    CGPoint pos;
    UIImageView *image;

} EnemyBall1,EnemyBall2;

Now in my ViewDidLoad method I want to add initialize the UIImageView of the struct with a .png and add it to the main view.

I tried to with this code:

[EnemyBall1.image initWithImage:[UIImage imageNamed:@"enemyball.png"]];

CGRect frame2 = [EnemyBall1.image frame];
frame2.origin.x = 140;
frame2.origin.y = 140;
[EnemyBall1.image setFrame:frame2];
[self.view addSubview:EnemyBall1.image];

However, this doesn't seem to add the UIImageView to the vie开发者_开发百科w as I don't see it when I run the code. Can someone point me in the right direction?


can you show where you allocate EnemyBall1.image?

[EnemyBall1.image initWithImage:[UIImage imageNamed:@"enemyball.png"]];

should this not be:

EnemyBall1.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"enemyball.png"]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜