2

我为不同的用例使用不同的故事板。我的问题是如何通过故事板标识符实例化故事板、视图控制器并将其连接到实际的视图控制器类?

这是我现在的位置:

public dynamic func loadingViewController() -> AnyObject {
    return TyphoonDefinition.withClass(LoadingViewController.self) {
        (definition) in

        definition.injectProperty("presenter", with: self.loadingPresenter())
    }

//        return TyphoonDefinition.withFactory(self.storyboard(), selector: "instantiateViewControllerWithIdentifier:") {
//            (factory) in
//            
//            factory.injectParameterWith("LoadingViewController")
//            
//        }
}

public dynamic func storyboard() -> AnyObject {
    return TyphoonDefinition.withClass(TyphoonStoryboard.self) {
        (definition) in

        definition.useInitializer("storyboardWithName:factory:bundle:") {
            (initializer) in

            initializer.injectParameterWith("Loading")
            initializer.injectParameterWith(self)
            initializer.injectParameterWith(NSBundle.mainBundle())
        }

        definition.scope = TyphoonScope.Singleton
    }
}

在这个例子中,故事板的名称是“Loading”,视图控制器是“LoadingViewController”,故事板标识符也是“LoadingViewController”。

任何帮助(或示例代码)将不胜感激!

4

1 回答 1

2

要创建一个 TyphoonDefinition 表示将从情节提要发出的 UIViewController:

为情节提要创建定义:

- (UIStoryboard *)storyboard
{
    return [TyphoonDefinition withClass:[TyphoonStoryboard class] 
        configuration:^(TyphoonDefinition *definition) {

        [definition useInitializer:@selector(storyboardWithName:factory:bundle:)
            parameters:^(TyphoonMethod *initializer) {
                [initializer injectParameterWith:@"StoryboardName"];
                [initializer injectParameterWith:self];
                [initializer injectParameterWith:[NSBundle mainBundle]];
            }];
    }];
}


如果您想在情节提要中实例化标记为“初始”控制器的控制器:

- (UIViewController *)initialControllerFromStoryBoard
{
    return [TyphoonDefinition withFactory:[self storyboard] 
        selector:@selector(instantiateInitialViewController)];
}


如果您想在情节提要中实例化另一个控制器:

- (UIViewController *)arbitraryControllerFromStoryboard
{
    return [TyphoonDefinition withFactory:[self storyboard] 
        selector:@selector(instantiateViewControllerWithIdentifier:)

        parameters:^(TyphoonMethod *factoryMethod) {
            [factoryMethod injectParameterWith:@"ViewControllerId"];
        }];
}

很抱歉在 Objective-C 中回复,但我目前还没有掌握 Swift 方面的知识。

于 2015-04-16T09:20:14.267 回答