我正在学习swift,并试图找出如何将视图控制器作为依赖项添加到我正在创建的类中。理想情况下,我想在初始阶段添加它,这样它就“可靠”
但我不太明白-所以我希望有人能帮我一路。我得到的错误是:属性“self.homeController”未在super.init调用时初始化
基本上我有一个类,它创建了一个覆盖,上面有一个弹出窗口,上面有一个UICollectionView,可以点击。当您单击该项时,菜单将显示动画,然后在完成时从homecontroller启动该功能。

class SettingLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


    private let homeController: HomeController

    func showSettings(){

        // window is the full device since the view is smaller due to nav
        if let window = UIApplication.shared.keyWindow {

            blackView.alpha = 0
            blackView.backgroundColor = UIColor(white: 0, alpha: 0.6)
            blackView.frame = window.frame // set RECT to the same size as device
            blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
            window.addSubview(blackView)
            window.addSubview(collectionView)
            let height:CGFloat = CGFloat(settings.count) * cellHeight
            let y = window.frame.height - height
            collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: window.frame.height / 2)

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                self.blackView.alpha = 1
                self.collectionView.frame = CGRect(x: 0, y: y, width: window.frame.width, height: window.frame.height / 2)

            }, completion: nil)

        }
    }

    @objc func handleDismiss(){
        UIView.animate(withDuration: 0.5,
           animations:{
                self.blackView.alpha = 0

            if let window = UIApplication.shared.keyWindow {
                self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: window.frame.height / 2)
            }

        }, completion: {_ in
            self.blackView.removeFromSuperview()
            self.homeController.showSettingsController()
        })
    }



    override init(){
        super.init()
        self.homeController = HomeController()
        collectionView.dataSource = self
        collectionView.delegate = self

        collectionView.register(SettingsCell.self, forCellWithReuseIdentifier: cellID)
    }
}

在homecontroller中,单击的菜单按钮将初始化设置控制器并告诉它显示:
let settingsLauncher = SettingLauncher()

    @objc func handleMore(){
        settingsLauncher.showSettings()
    }

    func showSettingsController(){
        let dummyController = UIViewController()
        navigationController?.pushViewController(dummyController, animated: true)
    }

最佳答案

错误是告诉您,在调用super.init()之前,必须已初始化homeController属性,但您没有这样做。
将代码更改为:

override init(){
    self.homeController = HomeController()
    super.init()
    collectionView.dataSource = self
    collectionView.delegate = self

    collectionView.register(SettingsCell.self, forCellWithReuseIdentifier: cellID)
}

关于ios - 类中的Swift 4 Viewcontroller依赖项注入(inject),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48054203/

10-11 17:24