我正在玩Foodtracker的苹果快速教程,并尝试用点击手势打开照片库.
// // 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 } }
无论如何,当模拟器启动时,我点击照片,图库视图不会显示.我甚至看不到我的调试信息.
我已经删除了插座和操作,并通过添加UIImageView Outlet再次完成整个过程,添加手势识别器,添加手势识别器的操作但仍然没有发生它应该发生的事情.
我发现了问题 - 我没有启用默认情况下设置为false的用户交互.