按下按钮后显示图像

时间:2012-09-14 15:37:44

标签: iphone ios xcode uiviewcontroller uiimage

我正在尝试更改背景颜色,并在按下按钮时在屏幕上显示图像。我可以改变颜色,但是我无法看到要显示的图像痕迹。我不知道如何将图像放在屏幕上。我正在使用iOS 5和故事板,但我还没有将UIImageView添加到故事板中,因为我希望它出现。

我的ViewController.h

#import <UIKit/UIKit.h>

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

@end

我的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

任何帮助将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:2)

您必须将图像视图添加到视图控制器的视图中。

- (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];
}

答案 1 :(得分:0)

您尚未将图片视图添加到主视图的任何位置。你可以这样做:

- (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];
}