绝对初学者的问题。我正在尝试制作一个应用程序,它可以通过横向滑动屏幕来切换一组图像。因此,作为普通案例,我尝试先显示图像,然后考虑如何使用 Action 在图像之间切换。我正在使用 XCode 4.2。
所以,这就是我目前所拥有的。我在 Storyboard中添加了一个 UIImageView,然后按 Ctrl+拖动到“.h”文件中以创建一个 Outlet,它看起来像这样:
SwitchViewController.h
#import <UIKit/UIKit.h>
@interface SwitchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
然后在“.m”文件中,我试图在 DidLoad 方法中将图像设置到 UIImageView。
SwitchViewController.m
#import "SwitchViewController.h"
@implementation SwitchViewController
@synthesize imageView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *plate1 = [UIImage imageNamed:@"plates1.tif"];
[imageView setImage:plate1];
}
- (void)viewDidUnload
{
[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
return YES;
return NO;
}
@end
现在,如果我尝试运行它,没有编译错误。
但是程序停止说,线程 1:程序接收到信号“SIGABRT”
最佳答案
引用您的实际问题,您将抛出此异常:
2012-01-26 22:04:40.728 Switch-a-Switch[548:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SwitchViewController 0x6840660> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key UIImageView.'
您似乎已将 UIImageView
连接到名为 UIImageView
的属性,而不是 imageView
。在 Interface Builder 中重做连接并确保从 imageView
属性拖到您的 UIImageView
实例。
关于objective-c - 如何在 XCode 中使用 UIView 显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9020518/