Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类中的Swift 4 Viewcontroller依赖项注入_Ios_Swift_Xcode_Dependency Injection - Fatal编程技术网

Ios 类中的Swift 4 Viewcontroller依赖项注入

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

我正在学习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)
}

错误告诉您必须在调用super.init()之前初始化homeController属性,但您没有这样做

将代码更改为:

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

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

在@supportoftruth和@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)
    }
}
my 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)
    }

在@supportoftruth和@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)
    }
}
my 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)
    }

太近了!谢谢你的回答,太接近了!但现在我只看到一个白色屏幕,应用程序出现错误。@SpencerBigum您必须使用xib或故事板初始化HomeController,才能为应用程序创建视图controller@Tj3n谢谢你的提醒。我现在唯一的问题是,在初始化它之后,Im'调用注入的控制器的方法-里面的代码似乎没有触发:let dummyController=UIViewController()navigationController?.pushViewController(dummyController,animated:true),它将在导航堆栈上弹出一个空的视图控制器。我想你可能对你是如何努力实现你的目标感到困惑。有机会上传你的项目或测试版本吗?太接近了!谢谢你的回答,太接近了!但现在我只看到一个白色屏幕,应用程序出现错误。@SpencerBigum您必须使用xib或故事板初始化HomeController,才能为应用程序创建视图controller@Tj3n谢谢你的提醒。我现在唯一的问题是,在初始化它之后,Im'调用注入的控制器的方法-里面的代码似乎没有触发:let dummyController=UIViewController()navigationController?.pushViewController(dummyController,animated:true),它将在导航堆栈上弹出一个空的视图控制器。我想你可能对你是如何努力实现你的目标感到困惑。是否有机会上传您的项目或测试版本?您还可以检查Typhone框架:您还可以检查Typhone框架: