1

我正在尝试更改背景颜色并在按下按钮时使图像出现在屏幕上。我可以改变颜色,但无法显示图像的痕迹。我也不知道如何将图像放在屏幕上。我正在使用 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

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

4

2 回答 2

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];
}
于 2012-09-14T15:41:00.453 回答
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];
}
于 2012-09-14T15:42:25.693 回答