iOS - Tap Gesture issue on image view - ios

I am slowly working my way through Apple's learn developing for Swift and I keep running into an issue with my tap gesture. I have re-created the project numbers times all with the same outcome.
I add a tap gesture to an image view and this should open the photo library from my computer. Nothing happens.
When I download and run the sample file from Apple everything works. When I copy and paste the code from Apple's to mine, again, nothing happens. I have gone through everything that I can but feel that I am missing something.
Here is my code. Apple's code is below:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// handle the text fields user input through delegate callbacks
nameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the Keyboard
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// dismiss the picker if user cancels
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set the photoviewimage to be the selected image
photoImageView.image = selectedImage
// Dismiss the picker
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the Keyboard
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only Allow pictures to be selected and not taken
imagePickerController.sourceType = .PhotoLibrary
// make sure the viewcontroller is notified when the user selects an image
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
Here is Apple's code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
Working Code from Anbu
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed"))
photoImageView.userInteractionEnabled = true
photoImageView.addGestureRecognizer(tapgesture)
super.viewDidLoad()
// handle the text fields user input through delegate callbacks
nameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the Keyboard
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// dismiss the picker if user cancels
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set the photoviewimage to be the selected image
photoImageView.image = selectedImage
// Dismiss the picker
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Actions
func imagepressed () {
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only Allow pictures to be selected and not taken
imagePickerController.sourceType = .PhotoLibrary
// make sure the viewcontroller is notified when the user selects an image
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
/* #IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed"))
photoImageView.userInteractionEnabled = true
photoImageView.addGestureRecognizer(tapgesture)
// Hide the Keyboard
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only Allow pictures to be selected and not taken
imagePickerController.sourceType = .PhotoLibrary
// make sure the viewcontroller is notified when the user selects an image
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
*/
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}

The easiest way I found that solved the issue is to click the checkbox that says "User Interaction Enabled" in the Attributes Editor for the Image View. Screenshot

do like
The reason ,by default the UIImageview userInteraction is false, so you need to enable manually
Step-1
let tapGesture = UITapGestureRecognizer(target:self, action:Selector("imagePressed"))
photoImageView.userInteractionEnabled = true // this line is important
photoImageView.addGestureRecognizer(tapGesture)
Step-2
func imagePressed()
{
//do Your action here whatever you want
}

Your code is perfectly fine. The problem is with the UIImageView in the story board. The user interaction option is unchecked.
So you just have to enable this option in storyboard.
Go to your story board.
Select your UIImageView (you named it photoimageview)
Now in “Attribute Inspector” look for user interaction enabled, it should be “checked”
Now run your project it will work.

Where have you added the UITapGestureRecognizer for the image view? If you added it in Storyboard, did you connect it to the selector selectImageFromPhotoLibrary:?

Related

Touched not working in Xcode simulator (apple official tutorial 'FoodTracker')

I'm working on an official Apple tutorial to learn iOS.
But I did the same, but the touch does not work in the simulator.
I tried turning Xcode off and on and copying and pasting the example file but it still doesn't touch. Or is there anything else you should try?
I need help.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoimageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
mealNameLabel.text = textField.text
}
//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as?
UIImage else {
fatalError("Expected a dictionary containing an image, but was provided folowing: \(info)")
}
// Set photoImageView to display the selected image.
photoimageView.image = selectedImage
// dismiss picker
dismiss(animated: true, completion: nil)
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
#IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
You should add isUserInteractionEnabled as true with Storyboard and add Tap Gesture Recognizer and link (or update this link) #IBAction func selectImageFromPhotoLibrary with Storyboard

Ios with UIImagePickerController cause SIGABRT error

I am very new to iOS development, and as I follow the instruction from here, although it already told me to add a privacy - photo library usage to my properties item inside the info.plist to solve the sigabrt error, and I already did add that, it still show the sigabrt error. I am currently running the code from a simulator. Is it because I run it in simulator instead of real device?
Here is the viewcontroller
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
nameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UITextFieldDelegte
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
mealNameLabel.text = textField.text
}
//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
//MARK: Actions
#IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
#IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
// Hide the keyboard
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
}

Xcode error when using tap gesture recognizer

I start following the Swift tutorial offerred by Apple. In one step using the tap gesture recognizer. When tapping the image, should link to the photoLibrary. But instead, the following message show:
I have used the sample code by the official answer and the same problem pops up. So I guess it is not the code issue?
Not sure, but if the error is because of tap gesture. You can try that :
let enableTap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(enableTap)
view.userInteractionEnabled = true
func handleTap(_ sender: UITapGestureRecognizer) {
print("Success!!!")
}
My code: (sorry it is too long for replying in comment)
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
}
/*override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} */
// MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField)
{
mealNameLabel.text = textField.text
}
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
{
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
else
{
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker
dismiss(animated: true, completion: nil)
}
// MARK: Actions
#IBAction func setDefaultLabelText(_ sender: UIButton)
{
mealNameLabel.text = "Default Text"
}
#IBAction func selectImageFromPhotoLibrary(_ sender: Any)
{
// Hide the keyboard
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// The type of imagePickerController.sourceType is known to be UIImagePickerControllerSourceType, which is an enumeration. This means you can write its value as the abbreviated form .photoLibrary instead of UIImagePickerControllerSourceType.photoLibrary.
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
}
Try this one
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGestureRecognizer(tap)
Then, you should implement the handler, which will be called each time when a tap event occurs:
func handleTap(sender: UITapGestureRecognizer? = nil) {
// handling code
}
So now calling your tap gesture recognizer event handler is as easy as calling a method handleTap()

How to group items from a larger set of items in Swift and excluding certain items? (Example FoodTracker App)

I completed the example FoodTracker App that is on the Apple Developer site and I would like to modify the FoodTracker slightly. So as of now, the app allows me to add a food item and rate it on a scale of 0-5. This food item and rating is then saved. However, I would like to modify the app by excluding any items rated 2 stars or less from the list of saved items. I am also trying to find a way to search for certain keywords in the user input such as "dairy" or "beef" and exclude these as well. The whole objective of the app is to allow users with stomach issues such as IBS to log foods and rate them on a scale of 0-5 with 0-2 stars providing the least comfort to the stomach. The app will then save these foods with 3-5 stars and exclude those with 0-2 stars. The app will then search for certain keywords in the excluded list such as "milk" "beef" and after a certain period of time, the user will then receive a response such as "John Doe you have excluded oranges, tomatoes, and soda from your diet and therefore it can be concluded that you have a sensitivity to citrus foods." I am not sure how complicated this is and how to develop the algorithm for this, but I will post the code below from the View Controller file. Once again this is the example FoodTracker app from the Swift App Developer site that I am attempting to modify. I am not sure if this was specific enough, so please let me know what I can do to clarify and any help is greatly appreciated! Thank you very much.
****CODE****
import UIKit
class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var photoImageView: UIImageView!
#IBOutlet weak var ratingControl: RatingControl!
#IBOutlet weak var saveButton: UIBarButtonItem!
/*
This value is either passed by `MealTableViewController` in
`prepareForSegue(_:sender:)` or constructed as part of adding a new meal
*/
var meal: Meal?
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
// set up views if editing an existing meal
if let existingMeal = meal {
navigationItem.title = existingMeal.name
nameTextField.text = existingMeal.name
photoImageView.image = existingMeal.photo
ratingControl.rating = existingMeal.rating
}
// enable save button only if text field has valid name
checkValidMealName()
}
// MARK: UITextFieldDelegate
func textFieldDidBeginEditing(textField: UITextField) {
// Disable save button while editing
saveButton.enabled = true
}
func checkValidMealName() {
// disable the save button if text field is empty
let text = nameTextField.text ?? ""
saveButton.enabled = !text.isEmpty
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
checkValidMealName()
navigationItem.title = textField.text
}
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Navigation
#IBAction func cancel(sender: UIBarButtonItem) {
// depending on style of presentation (modal or push), this view
// controller needs to be dismissed in 2 different ways
let isPresentingInAddMealMode = presentingViewController is UINavigationController
if isPresentingInAddMealMode {
dismissViewControllerAnimated(true, completion: nil)
}
else {
navigationController!.popViewControllerAnimated(true)
}
}
// configure a view controller before it's passed
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if saveButton === sender {
let name = nameTextField.text ?? ""
let photo = photoImageView.image
let rating = ratingControl.rating
// set meal to be passed to MealTableViewController after unwind segue
meal = Meal(name: name, photo: photo, rating: rating)
}
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
}
import UIKit
class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var photoImageView: UIImageView!
#IBOutlet weak var ratingControl: RatingControl!
#IBOutlet weak var saveButton: UIBarButtonItem!
/*
This value is either passed by `MealTableViewController` in
`prepareForSegue(_:sender:)` or constructed as part of adding a new meal
*/
var meal: Meal?
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
// set up views if editing an existing meal
if let existingMeal = meal {
navigationItem.title = existingMeal.name
nameTextField.text = existingMeal.name
photoImageView.image = existingMeal.photo
ratingControl.rating = existingMeal.rating
}
// enable save button only if text field has valid name
checkValidMealName()
}
// MARK: UITextFieldDelegate
func textFieldDidBeginEditing(textField: UITextField) {
// Disable save button while editing
saveButton.enabled = true
}
func checkValidMealName() {
// disable the save button if text field is empty
let text = nameTextField.text ?? ""
saveButton.enabled = !text.isEmpty
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
checkValidMealName()
navigationItem.title = textField.text
}
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Navigation
#IBAction func cancel(sender: UIBarButtonItem) {
// depending on style of presentation (modal or push), this view
// controller needs to be dismissed in 2 different ways
let isPresentingInAddMealMode = presentingViewController is UINavigationController
if isPresentingInAddMealMode {
dismissViewControllerAnimated(true, completion: nil)
}
else {
navigationController!.popViewControllerAnimated(true)
}
}
// configure a view controller before it's passed
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if saveButton === sender {
let name = nameTextField.text ?? ""
let photo = photoImageView.image
let rating = ratingControl.rating
// set meal to be passed to MealTableViewController after unwind segue
meal = Meal(name: name, photo: photo, rating: rating)
}
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
}
You can use the filter function to do that:
self.meal?.filter({ $0.rating > 2})
And also for filtering by keywords:
self.meal?.filter({ $0.name.rangeOfString("keyword") != nil })

Swift tutorial foodtracker: tap gesture method not called

I am playing around with the apple swift tutorial for the Foodtracker and try to open now the Photo library with the tap gesture.
//
// ViewController.swift
// FoodTracker
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var mealPhotoView: UIImageView!
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set photoImageView to display the selected image.
mealPhotoView.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
print("selectImage called!")
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
#IBAction func setDefaultLabelText(sender: AnyObject) {
nameLabel.text = "Default Meal name"
}
override func viewDidLoad() {
super.viewDidLoad()
// Handle the user input on textfield through delegate callback (self == this)
nameTextField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
nameLabel.text = textField.text
}
}
anyhow when the simulator starts up and I tap on the photo the library view is not shown. I even cannot see me debugging message.
I already did remove the outlets and actions and did the whole process again by adding the UIImageView Outlet, add the gesture recognizer, add the action of the gesturerecognizer but still not happening what it should.
I have found the problem - I did not enable the User Interaction which is set to false by default.

Resources