Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i push other viewcontroller in drawercontroller? #132

Open
SiuyuenChow opened this issue Dec 28, 2018 · 4 comments
Open

How can i push other viewcontroller in drawercontroller? #132

SiuyuenChow opened this issue Dec 28, 2018 · 4 comments

Comments

@SiuyuenChow
Copy link

SiuyuenChow commented Dec 28, 2018

How can i push other viewcontroller in drawercontroller?

@SiuyuenChow SiuyuenChow changed the title How can i push other viewcontroller in drawcontroller? How can i push other viewcontroller in drawercontroller? Dec 28, 2018
@Yancerva
Copy link

Yancerva commented Jan 3, 2019

Hi! SiuyuenChow, I wondering the same, and i find out how to set a Push other views using a protocol. like this:
1- create the protocol:

protocol NavigationProtocol: class {
func navigationProtocol(view: UIViewController)
}

2- Use Your MenuView to set the protocol instance

  • in the menuSide viewController class -

weak var navigationProtocol: NavigationProtocol?

3- set the protocol instance to pass the view using mainView.. this is very tricky because we go to the app delegate and tell what view will control the protocol function, and this will be the MainViewController... so, we go to the appDelegate. I put code if you not using storyboards and of course if you use storyboards... its really the "same thing"

I got this code on the app delegate... (NOT USING STORYBOARDS)

  • in the didFinishWithLaunchingWithOptions func -
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let mainView = ViewController()
    let navigation = UINavigationController(rootViewController: mainView)
    let menuView = MenuSideViewController()

    menuView.navigationProtocol = mainView <- this is we need

    menuViewController.mainViewController = navigation
    menuViewController.drawerViewController = menuView
    window?.rootViewController = menuViewController

(USING STORYBOARDS)

     let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        guard
            let mainView = mainStoryboard.instantiateViewController(withIdentifier: "NavigationMainId") as? UINavigationController,   
            let menuView = mainStoryboard.instantiateViewController(withIdentifier: "MenuViewId") as? MenuViewController else { return false }

        menuView.navigationProtocol = mainView         <- this is we need 
        "Set in the storyboard the navigation id in the UINavigationController
          - Identity Inspector
          - Identity
          - Storyboard Id "

        menuViewController.mainViewController = mainView
        menuViewController.drawerViewController = menuView
        window?.rootViewController = menuViewController
        window?.makeKeyAndVisible()

4- at this point we got an error in the appdelegate because we don't have the protocol implemented on the MainView.. so, we go to the MainView, or better, create a file called ProtocolMainVew(or the name of the main view you have, like this ProtocolMainViewController) and do this.

extension MainViewController: NavigationProtocol {
func navigationProtocol(view: UIViewController) {
navigationController?.pushViewController(view, animated: true)
}
}

5-Using the procotol to push the views...
if you have a tableview or collection view in your MenuSide...
go to de didSelectRow or didSelectitem in case of collectionView... and for example if we have 3 items in the table or collection, you can do something like this..

let item = indexPath.row
switch item {
case 0:
let view = SomeViewController()
navigationProtocol?.navigationProtocol(view: view)
case 1:
let view = OtherViewController()
navigationProtocol?.navigationProtocol(view: view)
default:
break
}
appDelegate?.menuViewController.setDrawerState(.closed, animated: true)

and that's all... but one thing I dont know how pop the views when I put the menu on the navigation buttons for each view I set in the menu, maybe with another protocol I guess, but with this the navigation always will be the MainView. if you find how pop the views if you put the menu in every view you call in the menuSide, please let me know, and cheers!

@SiuyuenChow
Copy link
Author

HI @Yancerva:
For now I've implemented it in the form of a notification

@Yancerva
Copy link

can you show how can I do that? using - NSNotification I guess?

@Millapptech
Copy link

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file project_name/KYDrawerController.swift
to set self._drawerConstraint.constant = 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants