使用UIImagePickerController进行NSException

时间:2014-05-17 12:38:31

标签: ios objective-c ipad ios7 uiimagepickercontroller

我正在Xcode 5中构建我的第一个IOS 7 iPad应用程序 - 但我需要一些帮助解决这个问题。

我关注this教程:

我不太明白作者的意思: "所以打开testPickerViewController.h,我们想在下面添加类引用。"

UINavigationControllerDelegate, UIImagePickerControllerDelegate>

我在这里获得了我的视图controller.h文件:

#import "ViewController.h"

@interface DetailViewController : ViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
UIImagePickerController *imgPicker;
IBOutlet UIImageView *customImage;
}

@property(nonatomic, retain)UIImagePickerController *imgPicker;

@end

我的视图controller.m文件:

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize imgPicker, customImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

// Init the image picker
self.imgPicker = [[UIImagePickerController alloc]init];
self.imgPicker.allowsEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)AddImage:(id)sender {
// Let the user add an image for the specific subject
[self presentModalViewController:self.imgPicker animated:YES];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo {
customImage.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

@end

我没有按照我在顶部编写的内容运行应用程序,导致main.m文件中出现NSException。

我在这里做错了什么?

修改

main.m

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char * argv[])
{
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

修改

2014-05-17 14:56:47.509 myApp [1424:60b] * 由于未捕获的异常终止应用&#39; NSUnknownKeyException&#39;,原因:&#39; [setValue :forUndefinedKey:]:此类不是键映像的键值编码。&#39;

1 个答案:

答案 0 :(得分:0)

检查Interface Builder(IB)中IBOutlet customImage的连接,并确保引用插座的名称和UIImageView匹配。您可以通过单击IB中的连接检查器进行检查(它是圆圈内的一个小箭头)。

在您发布链接的教程中,显示​​IBOutlet UIImageView声明为image。我假设您更改了IBOutlet的名称并忘记重新连接它。您还可以通过检查属性声明旁边的点是填充还是空来检查IBOutlet customImage是否已连接。填充=已连接且已清空=未连接。

IBOutlet的属性声明示例:

@property (weak, nonatomic) IBOutlet UIImageView * customImage;

此外,<UINavigationControllerDelegate, UIImagePickerControllerDelegate>是协议。通过将这些行添加到@interface声明,您可以非常简单地说明,&#34;我希望我的UIViewController符合UINavigationControllerDelegateUIImagePickerControllerDelegate。我还希望在发生某些事件时实现自己的代码。&#34;