MealViewController.swift 2.5 KB
Newer Older
J
John Hampton 已提交
1
//
J
John Hampton 已提交
2
//  MealViewController.swift
J
John Hampton 已提交
3 4 5 6 7 8 9
//  iOSTemplate
//
//  Created by John Hampton on 2/24/19.
//

import UIKit

J
John Hampton 已提交
10
class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
J
John Hampton 已提交
11 12 13
    
    // MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
J
John Hampton 已提交
14
    @IBOutlet weak var photoImageView: UIImageView!
15
    @IBOutlet weak var ratingControl: RatingControl!
J
John Hampton 已提交
16
    
J
John Hampton 已提交
17 18 19
    override func viewDidLoad() {
        super.viewDidLoad()

J
John Hampton 已提交
20 21 22
        // Handle the text field’s user input through delegate callbacks.
        nameTextField.delegate = self
    }
J
John Hampton 已提交
23

J
John Hampton 已提交
24 25 26 27 28 29 30 31 32 33
    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
    }
    
J
John Hampton 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    //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 the following: \(info)")
        }
        
        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage
        
        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    }
    
J
John Hampton 已提交
53
    // MARK: Actions
J
John Hampton 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    @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)
    }
    
J
John Hampton 已提交
69 70
}