Swinject / SwinjectStoryboard

Swinject extension for automatic dependency injection via Storyboard
MIT License
268 stars 141 forks source link

Swinject, injecting an object in a subclass of UICollectionView Or UIView #60

Closed chandrapal199 closed 6 years ago

chandrapal199 commented 7 years ago

I have created a subclass of UICollectionView and assigned this custom class into storyboard's collection view. I have to inject a shared object in that custom class.

// ViewController
class BZConnectSocialAcccountViewC: BZSignupWizardBase {

    @IBOutlet weak var collectionView: BZConnectSocialListView!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

// Sub Class
class BZConnectSocialListView: UICollectionView {

    var connectSocialAccountViewModeling: BZConnectSocialAccountViewModeling?
}

// View Model
protocol BZConnectSocialAccountViewModeling {
}

class BZConnectSocialAccountVM: BZConnectSocialAccountViewModeling {

 private var objApiManager: APIManagerModeling?
    init(_ apiManager: APIManagerModeling) {

        self.objApiManager = apiManager
    }
}

// API Manager

protocol APIManagerModeling {
}
class APIManager: SessionManager, APIManagerModeling {
}

// Injection

container.register(APIManagerModeling.self) { _ in
            APIManager()
        }.inObjectScope(.container)

container.register(BZConnectSocialAccountViewModeling.self) { r in
            BZConnectSocialAccountVM(r.resolve(APIManagerModeling.self)!)
        }

container.register(BZConnectSocialAcccountViewC.self) { r in

            return self.viewControllerFor(storyboard: self.storyboardSettings(className: BZConnectSocialAcccountViewC.self), identifier: BZConnectSocialAcccountViewC.className) as! BZConnectSocialAcccountViewC

            }.initCompleted { r, c in

                c.collectionView.connectSocialAccountViewModeling = r.resolve(BZConnectSocialAccountViewModeling.self)

        }

c.collectionView in getting nil

jakubvano commented 7 years ago

Hi @chandrapal199 this looks more like a problem with your storyboard setup than Swinject configuration.

It is hard to say without seeing your storyboard, although couple of things that come to mind:

chandrapal199 commented 7 years ago

It is properly connected in storyboard. Please check in screen shot. screen shot 2017-07-12 at 4 38 38 pm

Can you please give me some idea to inject a dependency on sub-class.

yoichitgy commented 6 years ago

@chandrapal199, do you still have the issue? Or did you resolve the issue yourself?

chandrapal199 commented 6 years ago

I took another way to handle this problem.

On Wed, Sep 20, 2017 at 5:01 PM, Yoichi Tagaya notifications@github.com wrote:

@chandrapal199 https://github.com/chandrapal199, do you still have the issue? Or did you resolve the issue yourself?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Swinject/SwinjectStoryboard/issues/60#issuecomment-330824277, or mute the thread https://github.com/notifications/unsubscribe-auth/ActdrRegOXGX5ulWZQYuQVegHab_Ylw1ks5skPehgaJpZM4OVL4K .

--

Regards,Chandrapal Singh

yoichitgy commented 6 years ago

Thanks for handling the problem👍