Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly and Storyboard-Created ViewControllers #85

Closed
fabb opened this issue Sep 22, 2013 · 9 comments
Closed

Assembly and Storyboard-Created ViewControllers #85

fabb opened this issue Sep 22, 2013 · 9 comments

Comments

@fabb
Copy link

fabb commented Sep 22, 2013

I'm trying to wrap my head around direct injection, assemblies and the like, but there's still much to grasp for me. For the beginning, how would I use DI with iOS storyboards where view controllers are generated implicitly by the system? Would I have to do something special in the prepareForSegue methods?

@jasperblues
Copy link
Member

There are two ways:

a) A simple way. Create a 'recipe' in either XML or assembly style. Obtain the controller from the story board. Tell Typhoon to inject dependencies.

b) A more stream-lined way. Use the TyphoonStoryBoard factory component. . . This way you just ask for the controller from Typhoon like a normal component.

I believe @eriksundin has some examples. Let me see if I can dig them up. . . Bump if you don't hear back within a few hours.

BTW: I'll be setting up a mailing list very soon.

@eriksundin
Copy link
Contributor

There is currently a -injectProperties: method on the component factory that you can use to apply dependency injection on a component after it has been instantiated, ex. by the story board.

I know the TyphoonStoryBoard has been discussed in #15 , not sure if that component was ever merged into the project though?

For now, you could subclass UIStoryboard and call the -injectProperties: on the view controller by overriding:

– instantiateInitialViewController
– instantiateViewControllerWithIdentifier:

// Example
- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier {
  id instantiatedViewController = [super instantiateViewControllerWithIdentifier:identifier];
  [[TyphoonComponentFactory defaultFactory] injectProperties:instantiatedViewController];
  return instantiatedViewController;
}

@jasperblues
Copy link
Member

@fabb, @eriksundin - apologies - I thought that I saw and evaluated a pull request TyphoonStoryBoard last week. . But digging around just now it appears that its still pending implementation. . . we'll get to it as soon as we can. .sorry for the oversight - I've been busy with two client projects, but will be spending more time on Typhoon soon.

Meantime, you can resolve a view controller from the StoryBoard and then inject dependencies. . . @fabb do you need some help with this?

@fabb
Copy link
Author

fabb commented Sep 22, 2013

For now, you could subclass UIStoryboard and call the -injectProperties: on the view controller by overriding:

Wow, this sounds awesome.

Still, as a DI beginner, one question: As far as I have understood, injecting would be used to inject global objects used throughout the application. Would the concept also be useful for the case a viewcontroller is being configured by the preceding view controller? I.e. View controller A can both be shown by Viewcontrollers B and C, and when B shows A, it configures it differently than when C shows A (i.e. show some additional toolbar button)?

Meantime, you can resolve a view controller from the StoryBoard and then inject dependencies. . . @fabb do you need some help with this?

You mean using instantiateViewControllerWithIdentifier:? Or with segues?

@eriksundin
Copy link
Contributor

Still, as a DI beginner, one question: As far as I have understood, injecting would be used to inject global objects used throughout the application. Would the concept also be useful for the case a viewcontroller is being configured by the preceding view controller? I.e. View controller A can both be shown by Viewcontrollers B and C, and when B shows A, it configures it differently than when C shows A (i.e. show some additional toolbar button)?

Exactly, the DI container will manage and wire up the dependencies between components in your application. Isolating the configuration to one place in your app. In the case you describe, my personal opinion, is that it sounds more like a closed context-configuration. Which I would put in the presenting controller.

So Typhoon helps you make sure the controller is set up properly and have every dependency and configuration it needs. Then you can give it more information about in what context it will be presented before it will happen.

@jasperblues
Copy link
Member

@eriksundin That is exactly right.

You might have some view controllers set up to have the following common components:

  • Service client
  • Authentication Manager
  • Local Database Adapter

And then at Runtime you'd want to provide, say a FlightDetails object from ViewControllerA to ViewControllerB, after having resolved it from Typhoon.

Up until a while ago this was the way I'd always done it. However, one of our uses recently opened a ticket about autofac-style paramerter injection. This means that Typhoon will build the component as-per the recipe, and also allow some parameters that you can pass is some arguments to the container at run-time, which will also be added to the component. . so that its ready for use in a single step.

There are a couple of benefits you can get by passing these arguments via the container, rather than just injecting them manually after resolving, as described in: #23 and #47 . . so this feature is in the backlog.

@jasperblues
Copy link
Member

moved this question to stackoverflow: http://stackoverflow.com/questions/18998759/typhoon-assembly-and-storyboard-created-viewcontrollers/18998809#18998809

. . and created a Typhoon tag there. (Thanks Erik for the suggestion).

@fatuhoku
Copy link
Contributor

Sorry — I've sort of just run into this and I don't quite understand what the recommended way of passing information from one view controller to the next is.

I just can't really imagine what the code would look like. @jasperblues Are you saying that TyphoonStoryboard users still have to use -prepareForSegue:sender: to manually inject data into a destinationViewController from a preceding one (for now), and that soon there will be a feature that obviates the need for this?

I've posted a separate question onto SO that deals with this very specific case.

http://stackoverflow.com/questions/27015438/how-do-you-pass-data-from-one-viewcontroller-to-the-next-with-typhoonstoryboard

@jasperblues
Copy link
Member

@fatuhoku Answered your question on SO, let us know if it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants