我在 iOS 中使用 Swift 编写代码。我正在使用 plist 和 Storyboard集成。
我有一个对象,它有一个委托(delegate)。我希望将此对象注入(inject)多个 View Controller (不是一次全部)并将此对象的委托(delegate)设置为对象被注入(inject)的 View Controller 。这应该在程序集中完成,还是应该在 ViewDidLoad 中手动设置委托(delegate)?如何在议会中做到这一点?这方面的最佳做法是什么?
谢谢。
最佳答案
我推荐使用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;
}];
}
但这仍然需要你的每个 View Controller 实现一个类似的方法:
- (void)injectSpaghettiMachine:(SpaghettiMachine *)machine
with:(id)delegate
{
machine.delegate = self;
}
. .如果 Controller 有一个共同的基类,上面的内容可能会有用。
关于ios - 台风注入(inject)和设置委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30067059/