2
0

MasterViewController.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // MasterViewController.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Alamofire
  23. import UIKit
  24. class MasterViewController: UITableViewController {
  25. @IBOutlet weak var titleImageView: UIImageView!
  26. var detailViewController: DetailViewController? = nil
  27. var objects = NSMutableArray()
  28. // MARK: - View Lifecycle
  29. override func awakeFromNib() {
  30. super.awakeFromNib()
  31. navigationItem.titleView = titleImageView
  32. }
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. if let split = splitViewController {
  36. let controllers = split.viewControllers
  37. if let
  38. navigationController = controllers.last as? UINavigationController,
  39. topViewController = navigationController.topViewController as? DetailViewController
  40. {
  41. detailViewController = topViewController
  42. }
  43. }
  44. }
  45. // MARK: - UIStoryboardSegue
  46. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  47. if let
  48. navigationController = segue.destinationViewController as? UINavigationController,
  49. 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. }