How do you pass data from one ViewController to the next with TyphoonStoryboard?

355 views Asked by At

I've got the simplest possible application powered by a storyboard.

Screenie

We've got a UINavigationController, and view controllers A and B.

A has a textfield that the user is supposed to type their name into. When the user touches a button, then it segues into B, showing "Hi, [NAME]!"

How do I pass the user-entered name from A to B with Typhoon?

With Typhoon, I presume that it lets me avoid using -prepareForSegue:sender:, which is just evil from a DI perspective because it requires coupling between what otherwise would be totally unrelated View Controllers. (e.g. viewControllerB.nameToDisplay = self.textField.text;)

1

There are 1 answers

1
Jasper Blues On

A nice feature of Typhoon is the ability to use your assemblies as a factory interface that mixes static and runtime dependencies, thus avoiding the boilerplate of creating a custom factory. This feature is called Runtime Arguments. However since Storyboards are being used to emit the view controllers it won't be possible to use:

  • Runtime arguments, as the storyboard interface has no concept of this.
  • Initializer injection, as VCs emitted from a storyboard use initWithCoder

Something else you can do is to create a mutable model object in your assembly with scope TyphoonScopeWeakSingleton

- (Person *)storyboardModel
{
    return [TyphoonDefinition withClass:[Person class] 
        configuration:^(TyphoonDefinition *definition) {

        definition.scope = TyphoonScopeWeakSingleton;
    }];
}

You can then inject this into the top view controller, and any subsequent children on this stack. When this stack is eventually popped, the model object will be cleaned up as well.


Since Storyboard created view controllers work best with just property dependencies you may like to use this approach along with Typhoon's (still private, but available on master) auto-wiring macros. This saves the time of creating an assembly registration and wiring rules:

@interface INFWelcomeController : UIViewController <INFWelcomeViewDelegate>

    @property (nonatomic, strong) InjectedClass(Person) model;
    @property (nonatomic, strong) InjectedProtocol(WebClient) client;

@end

Auto-wiring will also be available for integration test cases.