Swift 4 Viewcontroller依赖注入类

问题描述 投票:0回答:2

我正在快速学习并试图弄清楚如何将视图控制器作为依赖项添加到我正在创建的类中。理想情况下,我想在init阶段添加它,因此它是“可靠的”

但我无法弄明白 - 所以我希望有人可以帮助我。我得到的错误是:属性'self.homeController'未在super.init调用时初始化

基本上我有一个Class,它创建一个带有弹出窗口的叠加层,它有一个可点击的UICollectionView。当您单击该项目时,菜单会激活,然后完成从家庭控制器触发该功能。

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中,单击的菜单按钮初始化settingsController并告诉它显示:

let settingsLauncher = SettingLauncher()

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

    func showSettingsController(){
        let dummyController = UIViewController()
        navigationController?.pushViewController(dummyController, animated: true)
    }
ios swift xcode dependency-injection
2个回答
1
投票

该错误告诉您在调用super.init()之前必须初始化homeController属性。

将代码更改为:

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

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

1
投票

在@UpholderOfTruth和@ Tj3n的帮助下计算出来。发布下面的工作代码。

class SettingLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    let homeController: HomeController

...other UICollectionview functions

    // on Item click insdie UIViewCollection
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        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
            let setting = self.settings[indexPath.item]
            self.blackView.removeFromSuperview()

            // Call function from Injected ViewController
            self.homeController.showControllerSetting(setting: setting)
        })

    }

    init(controller: HomeController){
        homeController = controller
        super.init()
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(SettingsCell.self, forCellWithReuseIdentifier: cellID)
    }
}

我的HomeController中的函数初始化SettingsLauncher类

    lazy var settingsLauncher:SettingLauncher = {
        let launcher = SettingLauncher(controller: self)
        return launcher
    }()

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

    func showControllerSetting(setting: Setting){
        let dummyController = UIViewController()
        navigationController?.pushViewController(dummyController, animated: true)
    }
© www.soinside.com 2019 - 2024. All rights reserved.