台风注入和设置代表
Typhoon injection and setting delegate
我正在用 Swift 在 iOS 中编码。我正在使用 plist 和故事板集成。
我有一个有委托的对象。我希望将此对象注入多个视图控制器(不是一次全部)并将此对象的委托设置为对象被注入的视图控制器。这应该在程序集中完成,还是应该在 ViewDidLoad 中手动设置委托?如何在议会中做到这一点?这方面的最佳做法是什么?
谢谢。
我推荐使用typhoonDidInject
回调方式
typhoonDidInject() -> Void {
myObject.delegate = self;
}
或者如果您不想直接耦合到 Typhoon,请在程序集中指定一个自定义的:
definition.performAfterInjections("someMethod")
我想不出更简洁的方法来将所有这些连接到程序集中,但是如果您想就它的外观提出建议,我们可以实施。
编辑:
如果您想将所有这些都与 Typhoon 连接起来,您可以提供如下内容:
- (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;
}];
}
但这仍然需要您的每个视图控制器实现一个类似于以下的方法:
- (void)injectSpaghettiMachine:(SpaghettiMachine *)machine
with:(id)delegate
{
machine.delegate = self;
}
。 .如果控制器有一个共同的基础 class,以上内容可能会有用。
我正在用 Swift 在 iOS 中编码。我正在使用 plist 和故事板集成。
我有一个有委托的对象。我希望将此对象注入多个视图控制器(不是一次全部)并将此对象的委托设置为对象被注入的视图控制器。这应该在程序集中完成,还是应该在 ViewDidLoad 中手动设置委托?如何在议会中做到这一点?这方面的最佳做法是什么?
谢谢。
我推荐使用typhoonDidInject
回调方式
typhoonDidInject() -> Void {
myObject.delegate = self;
}
或者如果您不想直接耦合到 Typhoon,请在程序集中指定一个自定义的:
definition.performAfterInjections("someMethod")
我想不出更简洁的方法来将所有这些连接到程序集中,但是如果您想就它的外观提出建议,我们可以实施。
编辑:
如果您想将所有这些都与 Typhoon 连接起来,您可以提供如下内容:
- (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;
}];
}
但这仍然需要您的每个视图控制器实现一个类似于以下的方法:
- (void)injectSpaghettiMachine:(SpaghettiMachine *)machine
with:(id)delegate
{
machine.delegate = self;
}
。 .如果控制器有一个共同的基础 class,以上内容可能会有用。