Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection in IOS5 programming and Objective C

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.

  1. 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.

  2. How do I inject that object into the first UIViewController that will be shown using the storyboard?

  3. 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.

like image 356
Extrakun Avatar asked Dec 31 '25 20:12

Extrakun


1 Answers

You can select any Cocoa Dependency Injection Framework on GitHub.

like image 119
Ivan Dyachenko Avatar answered Jan 03 '26 12:01

Ivan Dyachenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!