1

I have been searching for how the delegate works and I tried to do it in my project. Unfortunately, the delegate method I implement does not get called ever. I am trying to do a slide-out navigation panel. so what I did is that I put two uicontainerviews, one is for slide-out navigation panel and the other for main view controller enter image description here

The code is that For main view controller

protocol MainViewControllerDelegate {
    func toggleSideMenu()
}

class MainViewController: UIViewController {

    var delegate: MainViewControllerDelegate?

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

    // MARK: - Slide Action

    @IBAction func slideMenuTapped(_ sender: UIBarButtonItem){
        delegate?.toggleSideMenu()
        print("Slide Menu has been tapped")
    }
}

For container view controller

class ContainerVC: UIViewController {
    @IBOutlet weak var SideMenuConstraint: NSLayoutConstraint!
    @IBOutlet weak var slideMenuContainer: UIView!
    @IBOutlet weak var mainViewContainer: UIView!

    var mainViewController: MainViewController?

    var isSideMenuOpened = false

    override func viewDidLoad() {
        super.viewDidLoad()

        mainViewController = UIStoryboard.mainViewController()
        mainViewController?.delegate = self
    }
}

extension ContainerVC: MainViewControllerDelegate{
    func toggleSideMenu() {
        print("It works")
        if isSideMenuOpened{
            isSideMenuOpened = false
            SideMenuConstraint.constant = -260
            mainViewContainer.layer.shadowOpacity = 0
        } else {
            isSideMenuOpened = true
            SideMenuConstraint.constant = 0
            mainViewContainer.layer.shadowOpacity = 0.59
        }
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }
}

extension UIStoryboard{
    static func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
    static func mainViewController() -> MainViewController? {
        return mainStoryboard().instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
    }
}

Please let know what's wrong

2 Answers2

0

Here is where you got wrong:

mainViewController = UIStoryboard.mainViewController()
mainViewController?.delegate = self

this mainViewController is not the same as the child of the container view controller, so setting its delegate doesn't really do anything.

You need to first get the VC that is the child of the container view controller:

mainViewController = self.childViewControllers.last as! MainViewController
mainViewController.delegate = self
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

I think the reason is that you embed your main view controller in navigation controller :

let navigationController = self.childViewControllers.last as! UINavigationController
let mainViewController = navigationController.topViewController as! MainViewController
mainViewController?.delegate = self
Qi Hao
  • 36
  • 3