MasterViewController.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // MasterViewController.swift
  3. //
  4. // Copyright (c) 2014-2016 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Alamofire
  25. import UIKit
  26. class MasterViewController: UITableViewController {
  27. @IBOutlet weak var titleImageView: UIImageView!
  28. var detailViewController: DetailViewController? = nil
  29. var objects = NSMutableArray()
  30. // MARK: - View Lifecycle
  31. override func awakeFromNib() {
  32. super.awakeFromNib()
  33. navigationItem.titleView = titleImageView
  34. }
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. if let split = splitViewController {
  38. let controllers = split.viewControllers
  39. if let navigationController = controllers.last as? UINavigationController,
  40. let topViewController = navigationController.topViewController as? DetailViewController
  41. {
  42. detailViewController = topViewController
  43. }
  44. }
  45. }
  46. // MARK: - UIStoryboardSegue
  47. override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
  48. if let navigationController = segue.destinationViewController as? UINavigationController,
  49. let detailViewController = navigationController.topViewController as? DetailViewController
  50. {
  51. func requestForSegue(_ segue: UIStoryboardSegue) -> Request? {
  52. switch segue.identifier! {
  53. case "GET":
  54. detailViewController.segueIdentifier = "GET"
  55. return Alamofire.request(.GET, "https://httpbin.org/get")
  56. case "POST":
  57. detailViewController.segueIdentifier = "POST"
  58. return Alamofire.request(.POST, "https://httpbin.org/post")
  59. case "PUT":
  60. detailViewController.segueIdentifier = "PUT"
  61. return Alamofire.request(.PUT, "https://httpbin.org/put")
  62. case "DELETE":
  63. detailViewController.segueIdentifier = "DELETE"
  64. return Alamofire.request(.DELETE, "https://httpbin.org/delete")
  65. case "DOWNLOAD":
  66. detailViewController.segueIdentifier = "DOWNLOAD"
  67. let destination = Alamofire.Request.suggestedDownloadDestination(
  68. directory: .cachesDirectory,
  69. domain: .userDomainMask
  70. )
  71. return Alamofire.download(.GET, "https://httpbin.org/stream/1", destination: destination)
  72. default:
  73. return nil
  74. }
  75. }
  76. if let request = requestForSegue(segue) {
  77. detailViewController.request = request
  78. }
  79. }
  80. }
  81. }