Swinject: Dependencies of non-root UIViewControllers are not getting storyboard injected

Created on 20 Jul 2016  路  6Comments  路  Source: Swinject/Swinject

Xcode version: 7.3
Swinject version: 1.1.1

I've got a series of view controllers (drill down tableview) laid out in a Storyboard. The initial view controller in the storyboard is a navigation controller, with ViewControllerA as the root view controller. ViewControllerA is a static table view controller, and when one of its cells is selected, a segue is launched for ViewControllerB (drilling down).

Both ViewControllerA and ViewControllerB are being injected with dependencies, but I'm only seeing ViewControllerA's factory getting called. I've also double checked that both view controllers have the Custom Class field set properly in IB.

This storyboard is referenced from my main storyboard (The main storyboard is a tab bar controller). Let me know if you require any more information.

Assembly:

class UIAssembly: AssemblyType {

    func assemble(container: Container) {
        container.registerForStoryboard(ViewControllerA.self) { r, c in
            c.dependency = r.resolve(Dependency.self)! // Gets run
        }
        container.registerForStoryboard(ViewControllerB.self) { r, c in
            c.depencency = r.resolve(Dependency.self)! // Does not get run
        }
    }
}

screen shot 2016-07-20 at 2 01 43 pm

bug help wanted

All 6 comments

Just re-read the documentation. I was using an Assembler to create my graph, and passing its resolver to the SwinjectStoryboard I was creating explicitly like so:

let storyboard = SwinjectStoryboard.create(name: "Main", bundle: nil, container: assembler.resolver)

Instead, I am now constructing my Assembler like so ...

class MainAssembler: Assembler {
    init() {
        super.init(container: SwinjectStoryboard.defaultContainer)

        applyAssemblies(...)
    }
}

... and not passing in a container to SwinjectStoryboard.create().

Not sure if this should still be a bug, but it was definitely unexpected behaviour, since all the initial view controllers of my storyboards were getting injected, despite being linked in through a reference.

Thanks @thalmicMark for sharing the issue馃槂

It looks a bug. I expected your assembly usage in the first comment injected view controller dependency, but actually not. Test cases for Swinject have only the following storyboard to test a navigation controller. The sequential case like yours should be added, and the issue should be fixed.

screen shot

This issue is caused by instantiation of UIStoryboard class which calls create without specifying Container. To resolve this issue use separate init method and set defaultContainer value with user passed Container.
To workaround this issue for now pod users should set defaultContainer value manually

@VladimirBorodko, thank you for investigating the issue馃槂 It is helpful to fix the issue!

This issue is fixed by PR #15 in SwinjectStoryboard repo for Swinject v2. It might be good to port the changes to Swinject v1.

Fixed by @jakubvano in pull request #144. It will be available in v1.1.4 soon.

Was this page helpful?
0 / 5 - 0 ratings