diff --git a/README.md b/README.md index 08e657c..a6c37b1 100644 --- a/README.md +++ b/README.md @@ -124,9 +124,23 @@ rootViewController.pushViewController(Inject.ViewControllerHost(viewController), let viewController = Inject.ViewControllerHost(YourViewController()) rootViewController.pushViewController(viewController, animated: true) ``` - > *Remember you **don't need** to remove this code when you are done, it's NO-OP in production builds.* + +#### **Injection Hook for UIKit** +depending on the architecture used in your UIKit App, you might want to attach a hook to be executed each time a view controller is reloaded. + +Eg. you might want to bind the `UIViewController` to the presenter each-time there's a reload, to achieve this you can use `onInjectionHook` + Example: + +```swift +myView.onInjectionHook = { hostedViewController in +//any thing here will be executed each time the controller is reloaded +// for example, you might want to re-assign the controller to your presenter +presenter.ui = hostedViewController +} +``` + #### iOS 12 You need to add -weak_framework SwiftUI to Other Linker Flags for iOS 12 to work. diff --git a/Sources/Inject/Integrations/Hosts.swift b/Sources/Inject/Integrations/Hosts.swift index 6066bef..224fb69 100644 --- a/Sources/Inject/Integrations/Hosts.swift +++ b/Sources/Inject/Integrations/Hosts.swift @@ -23,7 +23,18 @@ public typealias ViewHost = _InjectableViewHost open class _InjectableViewControllerHost: InjectViewControllerType { public private(set) var instance: Hosted let constructor: () -> Hosted - public var afterInjectionHook: (() -> Void)? + /// Attaches a hook to be executed each time after a controller is reloaded. + /// + /// Usage: + /// ```swift + /// let myView = ViewControllerHost(TestViewController()) + /// myView.onInjectionHook = { hostedViewController in + /// //any thing here will be executed each time the controller is reloaded + /// // for example, you might want to re-assign the controller to your presenter + /// presenter.ui = hostedViewController + /// } + /// ``` + public var onInjectionHook: ((Hosted) -> Void)? public init(_ constructor: @autoclosure @escaping () -> Hosted) { instance = constructor() @@ -34,8 +45,9 @@ open class _InjectableViewControllerHost: Inje addAsChild() onInjection { [weak self] instance in + guard let self else { return } instance.resetHosted() - self?.afterInjectionHook?() + self.onInjectionHook?(self.instance) } }