委托给 Storyboard 创建的 View Controller

delegate to Storyboard created View Controller

我创建了两个项目来了解委托方法是如何工作的.. 一个没有故事板的项目,仅通过代码创建,我的代表工作得很好。

我用故事板构建了另一个项目,这意味着所有 ViewControllers 在 Interfacebuilder 中都是可见的..

我确定问题在于代码文件中 ViewController 的定义:

let homeVC = HomeViewController()  

谁能告诉我这里出了什么问题?

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")
    }
}

错误: Debug_project/HomeViewController.swift:49: 致命错误:在展开可选值时意外发现 nil

我已经搜索了好几天了,就是找不到解决方案... 请帮帮我

我找到了解决办法! 感谢 Phillip Mills 和所有其他帮助我找到这个的人.. 解决方案是:

改变

let homeVC = HomeViewController()

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")
    }
}