-1

I'm trying to link my photo library to display an image in the UIImageView which will allow me to add filters, but when you select the photo and hit 'Choose' the app crashes. Below is the code I'm using; - (IBAction)chooseImage:(id)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.allowsEditing = YES;

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:nil];

}

The error is in the line:

"picker.delegate = self;" 

and is noted as *"Assigning to 'id' from incompatible type 'VSViewController const_strong'

Please if anyone has a suggestion on how to fix it or an alternate code that will operate, that would be appreciated.

Please try to avoid over complex language, I'm new to coding and this issue has been frustrating me for a while.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

2

Your class actually needs to conform to both UIImagePickerControllerDelegate as well as UINavigationControllerDelegate. This is done in the class declaration:

@interface VSViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
/* ... */
@end

The reason for this has been covered here: iPhone - UIImagePickerControllerDelegate inheritance

Community
  • 1
  • 1
nklauza
  • 1,501
  • 1
  • 12
  • 12
  • Where would the be entered in the project? The delegates or the .h or .m files? Do I need to add anything to the main file? – user3739491 Jun 14 '14 at 02:06
  • Is `VSViewController` a class that you've written? – nklauza Jun 14 '14 at 02:17
  • Do you mean is it the one that I'm coding in? – user3739491 Jun 14 '14 at 02:19
  • My assumption is that `VSViewController` is a class that you have created and that it has a corresponding header file and implementation file, such as `VSViewController.h` and `VSViewController.m`. Is that correct? – nklauza Jun 14 '14 at 02:21
  • I also assumed from the error you gave us as well as the code snippet you provided, that you are in fact coding within class methods of `VSViewController`. – nklauza Jun 14 '14 at 02:23
  • Yes, I'm coding in the VSViewcontroller.m file – user3739491 Jun 14 '14 at 02:26
  • All right. The most common place to put the protocol adoption is in the header file. So, in VSViewController.h, I assume you have a line that looks like this: `@interface VSViewController : UIViewController`. You need to alter that to match the code snippet I provided above. You can read more about Objective-C protocols here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html – nklauza Jun 14 '14 at 02:27
  • Thankyou very much! I was able to fix that particular issue! I really appreciate it. :) – user3739491 Jun 14 '14 at 02:33
  • For informational purposes, it is also possible to specify that the class conforms to a protocol in its implementation file instead, making its conformation private. – nklauza Jun 14 '14 at 02:35
  • You're welcome! And it seems that you're new to Stack Overflow, so hopefully you don't mind a friendly reminder to mark the best answer you received as accepted. See here for more information: http://stackoverflow.com/help/someone-answers – nklauza Jun 14 '14 at 02:40
1

In your header file, you need to conform to the protocol.

@interface YourViewController : UIViewController<UIImagePickerControllerDelegate>

and then you need to implement the protocol selectors.

Read this for reference: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • To be sure, which one was the header file? – user3739491 Jun 14 '14 at 02:04
  • the header file is the one that ends in `.h` Also, @David Xu, you need to update your code. UIImagePickerControllerDelegate requires the UINavigationController protocol conforming as well. – user2277872 Jun 14 '14 at 02:37