2012-09-14 112 views
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 

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

回答

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]; 
} 
+0

感谢您的澄清。这是完全合理的,现在相当明显。然而,我在[self.view addSubView:imageView]行上得到了@interface异常。我是否添加了UIImageView的界面? – Siriss

+0

它是'addSubview:'。 – DrummerB

+0

Ahhhh xcode没有纠正我,因为我有另一个错字。谢谢!这工作完美! – Siriss

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]; 
} 
+0

好主意。我喜欢隐藏的想法,但是如果我想添加其他内容,我可以将它放在ImageView上并隐藏它吗?再次感谢 – Siriss