2014-05-17 33 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