Reputation: 25
Trying to create an image picker with a tap gesture, so when the default picture is tapped it allows the user to pick an image from the device's photo library. For some reason when I click the view in the iOS simulator it simply doesn't do anything and I can't figure out why.
This is what I have so far. I'm pretty new to Objective-C
ViewController.m :
#import "ViewController.h"
interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *brandField;
@property (weak, nonatomic) IBOutlet UITextField *priceField;
@property (weak, nonatomic) IBOutlet UITextField *additionalNotesField;
@property (weak, nonatomic) IBOutlet UIButton *addItem;
@property (weak, nonatomic) IBOutlet UIButton *cancelAdditem;
@property (weak, nonatomic) IBOutlet UIImageView *toyImage;
- (IBAction)imageSelect:(UITapGestureRecognizer *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_nameField.delegate = self;
_brandField.delegate = self;
_priceField.delegate = self;
_additionalNotesField.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
-(BOOL) textFieldShouldReturn:(UITextField *) textField {
textField.resignFirstResponder;
return true;
}
-(void) textFieldDidEndEditing:( UITextField *) textField {
_nameField.text = textField.text;
_brandField.text = textField.text;
_priceField.text = textField.text;
_additionalNotesField.text = textField.text;
}
- (IBAction)imageSelect:(UITapGestureRecognizer *)sender {
// Hide the keyboard
_nameField.resignFirstResponder;
_brandField.resignFirstResponder;
_priceField.resignFirstResponder;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init]; // Creates image picker
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // Source for picker is saved photos. Only allow photos to be picked, not taken
// Make sure the picker is notified when the user picks an image
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:true completion:nil];
}
// UIImagePickerControllerDelegete
-(void) imagePickerDidCancel:(UIImagePickerController *) imagePicker {
[self dismissViewControllerAnimated:true completion:nil]; // Dimiss the picker if the user canceled
}
-(void) imagePicker:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* selectedImage = info[UIImagePickerControllerOriginalImage];
_toyImage.image = selectedImage; // Set the image view to display the selected image
// Dimiss the picker
[self dismissViewControllerAnimated:true completion:nil];
}
@end
Upvotes: 0
Views: 73
Reputation: 25
As Larme said, I needed to add this line:
_toyImage.userInteractionEnabled = YES;
Upvotes: 1