试试看看这个代码。
//YourController.h
@interface YourController : UIViewController
{
UIImageView* _photoImageView;
}
@property (retain, nonatomic) IBOutlet UIImageView *photoImageView;
- (IBAction)changeImageAction:(id)sender;
@end
//YourController.m
@synthesize photoImageView = _photoImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.photoImageView.image = [UIImage imageNamed:@"img.png"];
}
- (IBAction)changeImageAction:(id)sender {
self.photoImageView.image = [UIImage imageNamed:@"img_2.png"];
}
一些注意事项。
首先,您必须在XCode中正确链接您的photoImageView
IBOutlet。为了测试目的,我创建了一个链接到IBAction
的测试按钮(UIButton
)。 UIImageView
和测试按钮(我用IB创建的)都是YourController
视图的子视图。
请记得将您的图像导入到您的项目中并释放内存dealloc
和viewDidUnload
。
- (dealloc)
{
[_photoImageView release];
_photoImageView = nil;
[super dealloc]
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.photoImageView = nil;
}
希望它有帮助。
编辑
的代码不是圆弧定向但一些修改,有可能还与ARC运行它。
你是说如果你改变iVar的'照片',photoImageView中的图像也改变了?根据你迄今为止所展示的内容,它不应该这样做,但是你可能需要发布更多的代码,以便更好地了解发生了什么。您是直接更改iVar还是使用属性访问器?如果您使用的是属性访问器,它是如何实现的? – 2012-01-14 15:16:03