Advertisement
Guest User

Untitled

a guest
Jan 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. //
  2. // AddViewController.swift
  3. // mdb
  4. //
  5. // Created by Timar Cristian on 04/12/2017.
  6. // Copyright © 2017 Timar Cristian. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Firebase
  11. import FirebaseDatabase
  12. class AddViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
  13. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  14. return 1
  15. }
  16.  
  17. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  18. return years.count
  19. }
  20.  
  21. var movs = [Movie]()
  22. var path = ""
  23. var index = 0
  24. let picker = UIPickerView()
  25.  
  26. var ref: DatabaseReference!
  27.  
  28. @IBOutlet var movie_title: UITextField!
  29. @IBOutlet var movie_poster: UITextField!
  30. @IBOutlet var movie_desc: UITextField!
  31. @IBOutlet var movie_year: UITextField!
  32.  
  33. var years = ["2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017"];
  34.  
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. print(path)
  38. picker.delegate = self
  39. picker.dataSource = self
  40. movie_year.inputView = picker
  41. // Do any additional setup after loading the view.
  42.  
  43. ref = Database.database().reference()
  44.  
  45. }
  46.  
  47. override func didReceiveMemoryWarning() {
  48. super.didReceiveMemoryWarning()
  49. // Dispose of any resources that can be recreated.
  50. }
  51.  
  52.  
  53. @IBAction func addButton(_ sender: UIButton) {
  54. // && is_int(string: movie_year.text!)
  55. if(movie_title.text != "" && movie_poster.text != "" && movie_year.text != "" && movie_desc.text != ""){
  56. let new_movie = ["title": movie_title.text!, "year": movie_year.text!, "desc": movie_desc.text!, "poster": movie_poster.text!, "type": "movie"]
  57. let movie = self.ref.child("Movies").childByAutoId()
  58. movie.setValue(new_movie)
  59. //films.append(new_movie!)
  60. //NSKeyedArchiver.archiveRootObject(films, toFile: path)
  61.  
  62. let alert = UIAlertController(title: "Succes!", message: "Movie added with succes!", preferredStyle: UIAlertControllerStyle.alert)
  63.  
  64. let close = UIAlertAction(title: "Close", style: .default, handler: nil)
  65.  
  66. self.present(alert, animated: true, completion: nil)
  67. alert.addAction(close)
  68. print(movs.count)
  69. movie_title.text = ""
  70. movie_poster.text = ""
  71. movie_year.text = ""
  72. movie_desc.text = ""
  73. }
  74. }
  75. func is_int(string: String) -> Bool {
  76. return Int(string) != nil
  77. }
  78. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  79. return years[row]
  80. }
  81. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
  82. movie_year.text = years[row];
  83. self.view.endEditing(false)
  84. }
  85.  
  86. @IBAction func BackButton(_ sender: Any) {
  87. dismiss(animated: true, completion: nil)
  88. }
  89. /*
  90. // MARK: - Navigation
  91.  
  92. // In a storyboard-based application, you will often want to do a little preparation before navigation
  93. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  94. // Get the new view controller using segue.destinationViewController.
  95. // Pass the selected object to the new view controller.
  96. }
  97. */
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement