1
\$\begingroup\$

I am playing with 3 Main Classes Please have a look

I have integrated https://github.com/jonkykong/SideMenu

I have one tableview which act as leftViewController

This class is show the list of menu user has

import UIKit

struct SideMenuListItem {
    var name:String
    var isSelected: Bool
    var notificationCount:Int
    var viewController:UIViewController
}

protocol SideMenuItemTappedDelegate:class {
    func itemTapped(item:SideMenuListItem,index:Int)
}

class SideMenuListVC: UIViewController {

    //MARK:- Outlets

    @IBOutlet weak var tableView: UITableView!

    //MARK:- Var

    weak var delegate : SideMenuItemTappedDelegate?

    //Please ignore repetition  `viewController`  this is just for testing 
   private let menuItems = [SideMenuListItem(name: "Inbox", isSelected: false, notificationCount: 25,viewController:MapVC.viewController()),
                     SideMenuListItem(name: "Stats", isSelected: false, notificationCount: 0,viewController:PageContentViewController.viewController(pageData:PageData(title: "How to do", desc: "TEST STATS", image: UIImage(named: "Rectangle 2 copy")!))),
                     SideMenuListItem(name: "Calendar", isSelected: false, notificationCount: 12,viewController:MapVC.viewController()),
                     SideMenuListItem(name: "Map", isSelected: true, notificationCount: 0,viewController:MapVC.viewController()),
                     SideMenuListItem(name: "Settings", isSelected: false, notificationCount: 0,viewController:PageContentViewController.viewController(pageData:PageData(title: "How to do", desc: "TEST SETTINGS", image: UIImage(named: "Rectangle 2 copy")!)))]

    var getCurrentSelectedItem:SideMenuListItem? {
        return menuItems.first{$0.isSelected}
    }

    ... 

When User Login I call following class (root view controller) embedded in navigation controller

import UIKit
import SideMenu

class SideMenuVC: UIViewController {

    //--------------------------------------------------------------------------------

    //MARK:- Memory Managment Methods

    //--------------------------------------------------------------------------------

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    //--------------------------------------------------------------------------------

    //MARK:-  Custom Methdos

    //--------------------------------------------------------------------------------

    private func configureSideMenu() {
      AppGlobalManager.sharedManager.setupSideMenu()
       AppGlobalManager.sharedManager.leftMenuViewController.delegate = self

        if let vc =  AppGlobalManager.sharedManager.leftMenuViewController.getCurrentSelectedItem?.viewController {
          self.addChildVC(vc)
        }

    }

    private func addChildVC (_ vc:UIViewController) {
        self.addChild(vc)
        self.view.addSubview(vc.view)
        vc.view.frame = self.view.bounds
        vc.didMove(toParent: self)

    }

    //--------------------------------------------------------------------------------

    //MARK:- ViewLifeCycle Methods

    //--------------------------------------------------------------------------------


    override func viewDidLoad() {
        super.viewDidLoad()

        self.configureSideMenu()
    }

    //--------------------------------------------------------------------------------

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    //--------------------------------------------------------------------------------

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }



}

And This is AppGlobalManager Which is Shared class

import UIKit
import SideMenu
import Foundation

class AppGlobalManager: NSObject {

    static let sharedManager = AppGlobalManager()

   lazy var leftMenuViewController =   SideMenuListVC.viewController()

    public func setupSideMenu () {
        let sideMenu = UISideMenuNavigationController(rootViewController: leftMenuViewController)
        SideMenuManager.default.menuLeftNavigationController = sideMenu
        SideMenuManager.default.menuLeftNavigationController?.isNavigationBarHidden = true
        SideMenuManager.default.menuWidth = UIScreen.main.bounds.width
        SideMenuManager.default.menuPresentMode = .menuDissolveIn
        SideMenuManager.default.menuFadeStatusBar = false

    }

}

What I am doing :

  1. On Login set SideMenuVC to root View controller
  2. Setup Side menu controller from AppGlobalManager (shared instance )class
  3. Keep leftMenu Controller as global property
  4. Get Current Selected ViewController and add to Child VC
  5. Setup delegate of Menu list so when user tap on cell call addChildVC

Please suggest

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.