Siriss
Siriss

Reputation: 3767

Make image appear after button push

I am trying to change the background color and make an image appear on the screen when a button is pushed. I can get the color to change, but I can't get a trace of the image to appear. I don't know how to place the image on the screen either. I am using iOS 5 and storyboard, but I have not added the UIImageView to the storyboard, as I want it to appear.

My ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)changeButton:(id)sender;

@end

My ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)colorchange
{
   self.view.backgroundColor = [UIColor greenColor];
}

-(void)imageShow
{
    UIImage *image = [UIImage imageNamed:@"logo-tag-centered.pmg"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [imgView setImage:image];

}

- (IBAction)changeButton:(id)sender
{
    [self colorchange];
    [self imageShow];

}
@end

Any help would be greatly appreciated! Thanks!

Upvotes: 1

Views: 161

Answers (2)

DrummerB
DrummerB

Reputation: 40211

You have to add the image view to your view controller's view.

- (void)showImage
{
    UIImage *image = [UIImage imageNamed:@"logo-tag-centered.png"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    imgView.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);
    [self.view addSubview:imgView];
}

Upvotes: 2

Ravi
Ravi

Reputation: 8309

You haven't added the image view anywhere to the main view. You could do this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"logo-tag-centered.pmg"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [imgView setImage:image];
    [imgView setHidden:YES];
    [self.view addSubview:imgView];
}
-(void)imageShow
{
    [imgView setHidden:NO];
}
- (IBAction)changeButton:(id)sender
{
    [self colorchange];
    [self imageShow];
}

Upvotes: 0

Related Questions