daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  FoodTracker
  4. //
  5. //  Created by Oleksii Kulikov on 2/7/18.
  6. //  Copyright © 2018 Yeexel Inc. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  12.     //MARK: Properties
  13.     @IBOutlet weak var nameTextField: UITextField!
  14.     @IBOutlet weak var mealNameLabel: UILabel!
  15.     @IBOutlet weak var photoImageView: UIImageView!
  16.    
  17.     override func viewDidLoad() {
  18.         super.viewDidLoad()
  19.         nameTextField.delegate = self
  20.     }
  21.    
  22.     //MARK: UITextFieldDelegate
  23.     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  24.         // hide the keyboard
  25.         textField.resignFirstResponder()
  26.         return true
  27.     }
  28.    
  29.     func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
  30.         mealNameLabel.text = textField.text
  31.         textField.text = ""
  32.     }
  33.    
  34.     //MARK: UIImagePickerControllerDelegate
  35.     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  36.         dismiss(animated: true, completion: nil)
  37.     }
  38.    
  39.     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  40.         guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
  41.             fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
  42.         }
  43.        
  44.         photoImageView.image = selectedImage
  45.        
  46.         dismiss(animated: true, completion: nil)
  47.     }
  48.  
  49.     //MARK: Actions
  50.     @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
  51.         nameTextField.resignFirstResponder()
  52.        
  53.         let imagePickerController = UIImagePickerController()
  54.         imagePickerController.sourceType = .photoLibrary
  55.        
  56.         imagePickerController.delegate = self
  57.         present(imagePickerController, animated: true, completion: nil)
  58.     }
  59.    
  60.     @IBAction func setDefaultLabelText(_ sender: UIButton) {
  61.         mealNameLabel.text = "Default text"
  62.     }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top