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

Multiple Storyboards and Typhoon #301

Closed
etolstoy opened this issue Dec 23, 2014 · 6 comments
Closed

Multiple Storyboards and Typhoon #301

etolstoy opened this issue Dec 23, 2014 · 6 comments

Comments

@etolstoy
Copy link
Contributor

Hi!
I'm trying to implement multiple storyboards support in my app (different storyboards for each user story). I'm using custom segues (http://spin.atomicobject.com/2014/03/06/multiple-ios-storyboards/) for switching between different storyboards with Interface Builder. Besides, I don't want to be attached to Typhoon in my code, so calling:

+ (UIViewController *)sceneNamed:(NSString *)identifier {
    .....
    TyphoonStoryboard *storyboard = [TyphoonStoryboard storyboardWithName:storyboard_name factory:[TyphoonComponentFactory defaultFactory] bundle:nil];
    .....
}

instead of

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboard_name bundle:nil];

is not a very good option. Besides using TyphoonStoryboard in my own code, I should directly call [[TyphoonStartup initialFactory] makeDefault] in AppDelegate (or create a new assembly simultaneously with a storyboard).
So, I have a couple of questions:

  1. Why are you releasing InitialFactory after the ApplicationDidFinishLaunchingNotification? If there would be an instance of InitialFactory during the new storyboard initialization, then swizzling in TyphoonInitialStoryboardResolver would return proper TyphoonStoryboard instance.
  2. What are the best practices for using multiple storyboards along with Typhoon (without manual initialization)?
@jasperblues
Copy link
Member

Hi @igrekde !!

Don't use makeDefault

Instead of calling makeDefault it's better to inject the factory with. It can be injected onto any definition with:

[definition injectProperty:@selector(assembly) with:self];

This property can be of type TyphoonComponentFactory or posing as any of your assembly interfaces. In Objective-C (but not Swift) you can also cast from one to the other. To inject into the AppDelegate, you can create a special case definition for the AppDelegate:

- (AppDelegate *)appDelegate
{
    return [TyphoonDefinition withClass:[AppDelegate class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(assembly) with:self];
    }];
}

Although the app-delegate is not build by Typhoon, it sees you have a definition for the app delegate and injects accordingly. More info at plist integration

. . this provides cleaner decoupling, testability, etc than calling makeDefault, which is just for integrating Typhoon into legacy apps.

You can use one factory throughout your app or use it to load other factories.

Multiple Storyboards:

  • Declare your child storyboards in an assembly, injecting the factory as self, same as above.
  • Inject this storyboard where needed as type UIStoryboad. No coupling to Typhoon required.

Is this clear?

@jasperblues
Copy link
Member

Also, if you're keen, we have a new autowiring feature that works nicely with Storyboards and view controllers. It will be officially released in the pending Typhoon 3.0, but is available on master now. Interested in trying?

@etolstoy
Copy link
Contributor Author

@jasperblues Thanks for your fast reply. makeDefault was used just for proof-of-concept.
Well, I have nowhere to inject storyboards from my assembly. All I do for switching between storyboards is the following:

  1. Add a placeholder View Controller in the storyboard.
  2. Make a custom segue to this placeholder with name like "ViewControllerIdentifier_StoryboardName"
  3. In my custom segue class I parse this identifier, load storyboard with name "StoryboardName" and instantiate a view controller with identifier "ViewControllerIdentifier".

So, the approach with declaring storyboards in the assembly doesn't suit my case - because I'm initializing the child storyboard in the segue class, so I don't understand what and where to inject.

P.S. Autowire seems to be a great feature, and I'll definitely try it in my home project a bit later.

@jasperblues
Copy link
Member

@igrekde I think I misunderstood your question. @alexgarbarev will take it from here.

@alexgarbarev
Copy link
Contributor

Hi @igrekde,
I think you can solve your problem like this:

  1. Create definition for your placeholder ViewController, and inject the TyphoonComponentFactory factory as property ( the property can has type of TyphoonComponentFactory as well as your Assembly)
  2. In your custom segue, get reference to the factory, from the self.destinationViewController
  3. Instantiate any ViewController from typhoon factory (using another storyboard or not)

@etolstoy
Copy link
Contributor Author

Hi, @alexgarbarev ,
Thanks for your wonderful idea - it works greatly!

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

3 participants