简体   繁体   中英

UIImageView array placing in view iPhone?

Why does this code not work?

preview[currentPreview].frame = CGRectMake(0, 320, 480, 320);
preview[currentPreview].image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guy.png"]];

I am trying to place this inside my view, so that when a button is pressed, currentPreview changes and so does the image, but I cant seem to place this UIImageView on the iPhone screen.

I have declared preview[kNumberOfLevels]; in the same .m that this code is in (kNumberOfLevels = 30)

Thanks for any help!

EDIT WITH MORE CODE:

#define kNumberOfLevels             30

@implementation Chooser
@synthesize levelInput, completedLevels;
@synthesize btnInputGo, btnMenu, btnRight, btnLeft;

int currentPreview;
UIImageView *preview[kNumberOfLevels];

- (void)main {
    //NSString *imageName = [NSString stringWithFormat:@"guy.png"];//@"levelPreview%i", currentPreview];
    preview[currentPreview].frame = CGRectMake(0, 320, 480, 320);
    UIImage *img = [UIImage imageNamed:@"guy.png"];
    preview[currentPreview].image = [[UIImageView alloc] initWithImage:img];
    //[self addSubViews:preview[currentPreview].image];
    [self.view addSubViews:preview[currentPreview].image];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector (main) userInfo:nil repeats:YES];
    currentLevel = 1;
    currentPreview = 1;
    for (int i = 1; i <= kNumberOfLevels; i ++){
        NSString *key = [NSString stringWithFormat:@"keyLevel%i", i];
        levelComplete[i] = [[NSUserDefaults standardUserDefaults] stringForKey:key];
        levelCompleteInt[i] = [levelComplete[i] intValue];

        if (levelCompleteInt[i] == 1) {
            completedLevels.text = [NSString stringWithFormat:@"Completed Levels: %i", i];
        }
    }
}

Alloc preview[currentPreview] and then,

preview[currentPreview].frame = CGRectMake(0, 320, 480, 320);
UIImage *img = [UIImage imageNamed:@"guy.png"];
preview[currentPreview].image = [[UIImageView alloc] initWithImage:img];
[self addSubViews:preview[currentPreview].image];
[self.view addSubViews:preview[currentPreview].image];

EDIT: more code:

#define kNumberOfLevels             30

@implementation Chooser
@synthesize levelInput, completedLevels;
@synthesize btnInputGo, btnMenu, btnRight, btnLeft;

int currentPreview;
UIImageView *preview[kNumberOfLevels];

- (void)main {
    //NSString *imageName = [NSString stringWithFormat:@"guy.png"];//@"levelPreview%i", currentPreview];
    preview[currentPreview].frame = CGRectMake(0, 320, 480, 320);
    UIImage *img = [UIImage imageNamed:@"guy.png"];
    preview[currentPreview].image = [[UIImageView alloc] initWithImage:img];
    //[self addSubViews:preview[currentPreview].image];
    [self.view addSubViews:preview[currentPreview]];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector (main) userInfo:nil repeats:YES];
    currentLevel = 1;
    currentPreview = 1;
    for (int i = 1; i <= kNumberOfLevels; i ++){
        NSString *key = [NSString stringWithFormat:@"keyLevel%i", i];
        levelComplete[i] = [[NSUserDefaults standardUserDefaults] stringForKey:key];
        levelCompleteInt[i] = [levelComplete[i] intValue];

        if (levelCompleteInt[i] == 1) {
            completedLevels.text = [NSString stringWithFormat:@"Completed Levels: %i", i];
        }
    }
}

May be, You are not adding UIImageView in your view Or Controller's view .

Considering preview[currentPreview].image is type of UIImageView .

preview[currentPreview].frame = CGRectMake(0, 320, 480, 320);
preview[currentPreview].image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guy.png"]];
[self addSubView:preview[currentPreview].image]; //Add this line if you are in UIView inherited class 
[self.view addSubView:preview[currentPreview].image]; //Add this line if you are in UIViewController  inherited class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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