2014-05-17 25 views
-2

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

我下面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年5月17日14:56:47.509对myApp [1424:60B] *终止应用程序由于未捕获的异常 'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:此类不是关键图像的关键字编码兼容值。'

+1

而异常消息是...? –

+0

以NSException类型的未捕获异常终止 – Erik

+0

在这种情况下,您是否放置了断点并检查哪一行导致异常? –

回答

0

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

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

实施例:

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

另外,<UINavigationControllerDelegate, UIImagePickerControllerDelegate>是协议。通过将这些行添加到@interface声明中,您非常简单地指出:“我希望我的UIViewController符合UINavigationControllerDelegateUIImagePickerControllerDelegate。我还希望在发生某些事件时实现自己的代码。”

+0

我检查了链接,它是正确的。我稍微改变了我的.h代码,看到我的更新 – Erik

+1

我从ImageView中删除了图像链接并突然启动 – Erik

+0

非常棒。我很高兴现在一切都为你工作。继续阅读教程,你会得到一些东西。 – Jonathan