Advertisement
Guest User

Navigation Controller Nil

a guest
Apr 5th, 2020
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.99 KB | None | 0 0
  1. // This is for a Slide Menu
  2. // Container Controller
  3.  
  4. import Foundation
  5. import UIKit
  6.  
  7. class ContainerController: UIViewController {
  8.     // MARK: Properties
  9.     private var aboutController: AboutViewController!
  10.     private var slideController: SlideController!
  11.     private var homeController: HomeScreenController!
  12.     public var homeNavigationController: UINavigationController?
  13.     private var centerController: UIViewController!
  14.     // Delegates
  15.     public weak var blurEffectBooleanDelegate: BlurEffectBooleanProtocol?
  16.     public weak var homeCellSelectionDelegate: HomeCellSelectionProtocol?
  17.     // Slide Menu Toggle
  18.     private var isOpen: Bool = false
  19.    
  20.     override func viewDidLoad() {
  21.         super.viewDidLoad()
  22.         setup()
  23.         configureHomeNavigationController()
  24.     }
  25.  
  26.    
  27.     private func setup() {
  28.         view.backgroundColor = UIColor(red: 65/255, green: 0, blue: 0, alpha: 1.0)
  29.     }
  30.    
  31.    
  32.     private func configureHomeNavigationController() {
  33.         homeController = HomeScreenController()
  34.         homeController.handleSlideMenuDelegate = self
  35.         self.homeCellSelectionDelegate = homeController
  36.         homeNavigationController = UINavigationController(rootViewController: homeController)
  37.         centerController = homeNavigationController
  38.         view.addSubview(centerController.view)
  39.         addChild(centerController)
  40.         centerController.didMove(toParent: self)
  41.     }
  42.    
  43.    
  44.     private func configureAboutController() {
  45.         aboutController = AboutViewController()
  46.         view.insertSubview(aboutController.view, at: 0)
  47.         addChild(aboutController)
  48.         aboutController.didMove(toParent: self)
  49.     }
  50.    
  51.     // MARK: TEST - Status: DONE
  52.     fileprivate func addSlideMenu() {
  53.         if slideController == nil {
  54.             slideController = SlideController()
  55.             view.insertSubview(slideController.view, at: 0)
  56.             addChild(slideController)
  57.             slideController.didMove(toParent: self)
  58.         }
  59.     }
  60.    
  61.     // MARK: TEST - Status: DONE
  62.     fileprivate func removeSlideMenu() {
  63.         if slideController != nil {
  64.             slideController.view.removeFromSuperview()
  65.             slideController.willMove(toParent: nil)
  66.             slideController.removeFromParent()
  67.             slideController = nil
  68.         }
  69.     }
  70.    
  71.    
  72.     public func shouldExpandSlideMenu(_ expanded: Bool) {
  73.         if expanded {
  74.             UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
  75.                 self.centerController.view.frame.origin.x = self.centerController.view.frame.width - 100
  76.                 self.blurEffectBooleanDelegate?.blurEnabled = false
  77.                 self.addSlideMenu()
  78.             }, completion: nil)
  79.         } else {
  80.             UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
  81.                 self.centerController.view.frame.origin.x = 0
  82.                 self.blurEffectBooleanDelegate?.blurEnabled = true
  83.             }, completion: { _ in
  84.                 self.removeSlideMenu()
  85.             })
  86.         }
  87.     }
  88.    
  89. }
  90.  
  91. extension ContainerController: HandleSlideMenuProtocol {
  92.     func handleSlideToggle() {
  93.         isOpen = !isOpen
  94.         shouldExpandSlideMenu(isOpen)
  95.     }
  96.    
  97. }
  98.  
  99. extension ContainerController: InnerSelectedCellProtocol {
  100.     func selectedCell(isSelected: Bool) {
  101.         if isSelected == true {
  102.             print("Pushing")
  103.             homeController = HomeScreenController()
  104.             self.homeCellSelectionDelegate = homeController
  105.             homeCellSelectionDelegate?.pushToDetailController()
  106.         }
  107.     }
  108. }
  109.  
  110. // My testing Protocol to see if it would work.
  111. import Foundation
  112. import UIKit
  113.  
  114. protocol HomeCellSelectionProtocol: class {
  115.     func pushToDetailController()
  116. }
  117.  
  118.  
  119. // Home Controller which contains the collectionView which I try to push when I select an item. It would push before I refactored it to add the slide menu. Unrelated functions and properties removed. The navigationController isn't nil only until I'm trying to pushViewController()
  120.  
  121. import UIKit
  122. import Foundation
  123.  
  124. class HomeScreenController: UIViewController, BlurEffectBooleanProtocol, HomeCellSelectionProtocol {
  125.     // References / Properties
  126.     public lazy var detailController = DetailViewController()
  127.     public lazy var launchScreenController = LaunchScreenController()
  128.     public lazy var containerController = ContainerController()
  129.     // Delegates
  130.     public weak var launchScreenDelegate: LaunchScreenProtocol?
  131.     public weak var castMovieIdDelegate: CastMovieIdProtocol?
  132.     public weak var handleSlideMenuDelegate: HandleSlideMenuProtocol?
  133.    
  134.     override func viewDidLoad() {
  135.         super.viewDidLoad()
  136.         initialSetup()
  137.         makeApiCall()
  138.     }
  139.    
  140.    
  141.     private func makeApiCall() {
  142.         launchScreenDelegate = launchScreenController
  143.         navigationController?.pushViewController(launchScreenController, animated: false)
  144.         makeRequestToServer()
  145.     }
  146.    
  147.    
  148.     private func makeRequestToServer() {
  149.         apiManager.makeApiRequest {
  150.             self.leftBarButtonView()
  151.             self.mainView.refreshControl.endRefreshing()
  152.             self.launchScreenDelegate?.isLoadingFinished(true)
  153.             DispatchQueue.main.async { [weak self] in
  154.                 guard let self = self else { return }
  155.                 for cell in self.mainView.collectionView.visibleCells {
  156.                     if let cell = cell as? MovieCollectionViewCell {
  157.                         cell.innerCollectionView.reloadData()
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.     }
  163.    
  164.     // MARK: - Setup
  165.     private func initialSetup() {
  166.         // Navigation Controller
  167.         navigationController?.isNavigationBarHidden = false
  168.         let titleAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  169.         navigationController?.navigationBar.titleTextAttributes = titleAttributes
  170.         navigationController?.navigationBar.topItem?.title = "Movies"
  171.         navigationController?.navigationBar.prefersLargeTitles = true
  172.         navigationController?.hidesBarsOnSwipe = false
  173.         navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Thin", size: 50)!, NSAttributedString.Key.foregroundColor: UIColor(red: 225/255, green: 225/255, blue: 225/255, alpha: 1.0)]
  174.         navigationController?.navigationItem.leftBarButtonItem = nil
  175.         navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  176.         navigationController?.navigationBar.shadowImage = UIImage()
  177.         navigationController?.navigationBar.isTranslucent = true
  178.         // Delegates
  179.         containerController.blurEffectBooleanDelegate = self
  180.         // Blur Effect
  181.         blurEffectView.effect = mainView.blurEffect
  182.         blurEffectView.frame = view.bounds
  183.         blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  184.         blurEffectView.isHidden = blurEnabled
  185.         blurEffectView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(blurEffectTap)))
  186.         view.insertSubview(blurEffectView, at: 1)
  187.         // Refresh Control
  188.         mainView.refreshControl.addTarget(self, action: #selector(refreshView(_:)), for: .valueChanged)
  189.     }
  190.    
  191.    
  192.     private func leftBarButtonView() {
  193.         navigationItem.leftBarButtonItem = UIBarButtonItem(image: mainView.categoryImageView.image, style: .plain, target: self, action: #selector(categoryAction))
  194.     }
  195.    
  196.     // MARK: Delegate Function for Selection of Cell
  197.     func pushToDetailController() {
  198.         print("Inside")
  199.         displayingDetailController()
  200.     }
  201.    
  202.    
  203.     private func displayingDetailController() {
  204.         // The navigationController is nil
  205.         navigationController?.pushViewController(detailController, animated: true)
  206.     }
  207.    
  208. } // Class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement