Reputation: 19325
Say I have a library or service which is created by an application, and my UIViewControllers are served from the storyboard. I would like to use dependency injection to pass those objects to the ViewControllers as they are being created.
Where is the best place to define a service or class that holds commonly used utility functions? I could use a singleton but I am exploring other options, such as creating it in the first UIViewController that will be shown. The object is likely to be some logic that are used among different UIViewControllers, such as accessing the database, or access to a model shared between different controllers, or business/game logic.
How do I inject that object into the first UIViewController that will be shown using the storyboard?
When performing a segue, is there some way to do call an initializer of the next UIViewController to pass in a dependency? As far as I know, by the time the code reaches prepareForSeqgue, the next UIViewController to be shown has already been created.
Basically, I looking to do something like this:
-(void) hypotheticalApplicationStart
{
self.gameLogic = [[GameLogic alloc] init];
}
And then when the first UIViewController is created and push to be viewed:
-(void) hypotheticalDisplayFirstViewController
{
UIViewController* controller = [[ MyView alloc] int];
[controller setGameLogic:self.gameLogic)];
// display controller
}
*Sorry for any code typos or syntax error, I have just began learning Objective C and IOS5 programming.
Upvotes: 3
Views: 535
Reputation: 1348
You can select any Cocoa Dependency Injection Framework on GitHub.
Upvotes: 2