简体   繁体   中英

Typhoon injection and setting delegate

I'm coding in iOS with Swift. I am using plist and storyboard integration.

I have an object which has a delegate. I wish to inject this object into several view controllers (not all at once) and set the delegate of this object to be the view controller which the object was injected into. Should this be done in an Assembly or should I set the delegate manually in ViewDidLoad? How can this be done in an Assembly? What is the best practice for this?

Thank you.

I recommend using the typhoonDidInject callback method

typhoonDidInject() -> Void {
    myObject.delegate = self;
}

or if you don't wish to couple directly to Typhoon, specify a custom one in the assembly:

definition.performAfterInjections("someMethod")

I can't think of a neater way to wire all of this up in the assembly, however if you would like to make a suggestion of what it should look like, we could implement that.

Edit:

If you wanted to wire all of it up with Typhoon, you could provide something like:

- (UIViewController *)someViewController
{
   return [TyphoonDefinition withClass:[FoobarViewController class]    
       configuration:^(TyphoonDefinition *definition) {

        [definition injectProperty:@selector(machine) 
            with:[self spaghettiMachine]];
        [definition performAfterInjections:@selector(injectSpaghettiMachine:with:) 
            parameters:^(TyphoonMethod *method) {

            [method injectParameterWith:[self spaghettiMachine]];

            //This will be the same object for any scope except 'Prototype'
            [method injectParameterWith:[self someViewController]];
        }];
    }];
}


- (SpaghettiMachine *)spaghettiMachine
{
    return [TyphoonDefinition withClass:[SpaghettiMachine class]     
        configuration:^(TyphoonDefinition *definition) {

        definition.scope = TyphoonScopeWeakSingleton;
    }];
}

But this would still require that each of your view controller implement a method something like:

- (void)injectSpaghettiMachine:(SpaghettiMachine *)machine    
    with:(id)delegate
{
    machine.delegate = self;
}

. . the above might be useful if the controllers have a common base class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM