Advertisement
Guest User

Navigation Controller Nil in Delegate Function

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