Home > Blockchain >  delegate to Storyboard created View Controller
delegate to Storyboard created View Controller

Time:01-29

i created two projects to learn how the delegate method is working.. one project created WITHOUT storyboard, just via code and my delegate is working just fine.

i built the other Project WITH storyboard, which means all ViewControllers are visible in the Interfacebuilder..

i am sure the issue lays in the definition of the ViewControllers in the code file:

let homeVC = HomeViewController()  

Can someone please tell what is wrong here?

import UIKit


protocol HomeViewControllerDelegate: AnyObject {
    func showMenu()
}


class HomeViewController: UIViewController {
    
    var delegate: HomeViewControllerDelegate?
   
    override func viewDidLoad() {
    title = "App"
        super.viewDidLoad()
        configureNaviBar()
        
    }
    
    func configureNaviBar() {

        // Left Bar Button Item
        let burgerButton = UIImage(systemName: "line.horizontal.3")
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: burgerButton, style: .plain, target: self, action: #selector(showMenu))
    }
    
    @objc func showMenu(sender: AnyObject) {
        print("show Menu (home)")
        // homeDelegate is nil?
        
        delegate!.showMenu()    // throws an error!
    }
}

import UIKit

class MainViewController: UIViewController {
    
    let naviVC:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NaviVC") as! NaviVC
    let menuVC:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SideMenuID") as! SideMenuViewController
    let homeVC = HomeViewController()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .green
        setupContainerView()
    }
        
    func setupContainerView() {
        // menu
        addChild(menuVC)
        self.view.addSubview(menuVC.view)
        menuVC.view.frame = CGRect(x: 0, y: 0, width: 200, height: 896)
        menuVC.didMove(toParent: self)
        
        // Home
        homeVC.delegate = self
        addChild(naviVC)
        self.view.addSubview(naviVC.view)
        naviVC.view.frame = self.view.bounds
        naviVC.didMove(toParent: self)
    }
}


extension MainViewController: HomeViewControllerDelegate {
    func showMenu() {
        // does not get called
        print("did tap menu")
    }
}

Error: Debug_project/HomeViewController.swift:49: Fatal error: Unexpectedly found nil while unwrapping an Optional value

i am already searching for days now, and just can't find the solution for this... please help me out guys

CodePudding user response:

I found the solution! Tanks to Phillip Mills and all others for helping me find this.. the solution is:

change

let homeVC = HomeViewController()

to

override func viewDidLoad() {
        super.viewDidLoad()
let homeVC = naviVC.viewControllers.first as! HomeViewController    // working: this is it!
}
class MainViewController: UIViewController {
    
    let naviVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NaviVC") as! NaviVC
    let menuVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SideMenuID") as! SideMenuViewController


    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .green
        setupContainerView()
    }
        
    func setupContainerView() {
        // menu
        addChild(menuVC)
        self.view.addSubview(menuVC.view)
        menuVC.view.frame = CGRect(x: 0, y: 0, width: 200, height: 896)
        menuVC.didMove(toParent: self)
        
        // Home
        let homeVC = naviVC.viewControllers.first as! HomeViewController    // working: this is it!
        homeVC.delegate = self
        addChild(naviVC)
        self.view.addSubview(naviVC.view)
        naviVC.view.frame = self.view.bounds
        naviVC.didMove(toParent: self)
    }
}


extension MainViewController: HomeViewControllerDelegate {
    func showMenu() {
        // does not get called
        print("did tap menu")
    }
}
  •  Tags:  
  • Related