Reputation: 3108
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];
}
}
}
Upvotes: 0
Views: 652
Reputation: 10344
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];
}
}
}
Upvotes: 1
Reputation: 31722
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
Upvotes: 1