gpt4 book ai didi

ios - 台风不注入(inject)属性(property)(无 Storyboard)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:26 24 4
gpt4 key购买 nike

我无法使用带有 initWithNibName:bundle: 的 XIB 将属性注入(inject)到 View Controller 中

例子:

这是我的程序集:

@implementation AppAssembly

- (ViewControllerC *)viewControllerC
{
return [TyphoonDefinition withClass:[ViewControllerC class]
configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(name) with:@"Injected string"];
}];
}

@end

ViewControllerA代码:

@implementation ViewControllerA

- (IBAction)buttonAction:(id)sender
{
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
[self.navigationController pushViewController:viewControllerB animated:YES];
}

@end

ViewControllerB代码:

@implementation ViewControllerB

- (IBAction)buttonAction:(id)sender
{
ViewControllerC *viewControllerC = [[ViewControllerC alloc] initWithNibName:@"ViewControllerC" bundle:nil];
[self.navigationController pushViewController:viewControllerC animated:YES];
}

@end

ViewControllerC代码:

@interface ViewControllerC : UIViewController

@property (copy, nonatomic) NSString *name;

@end

@implementation ViewControllerC

- (void)viewDidLoad
{
[super viewDidLoad];

// Why is this not being injected?
self.title = self.name;
}

@end

AppDelegate代码:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewControllerA *rootViewController = [[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

return YES;
}

@end

我已经检查了示例,它们与这种方法不同。你能告诉我我做错了什么吗?

谢谢。

最佳答案

Typhoon 是一个依赖注入(inject)容器,这意味着它不会检测、混合或以其他方式触及您的类。因此,要获得注入(inject)依赖项的类的实例,我们必须询问 Typhoon。

为什么不使用 Storyboard?

当使用 plist 集成时,Typhoon 注册 TyphoonStoryboard 代替 UIStoryboard 以根据 Storyboard中的定义发出实例 - 就像普通的 Storyboard一样工作,具有注入(inject)依赖项的额外好处。

在其他地方,为了获得注入(inject)依赖的实例,我们使用汇编接口(interface),并询问Typhoon。

解决步骤:

在您的程序集中添加 ViewControllerB 的定义。 (在 Objective-C 中,您也可以根据需要使用自动注入(inject)宏)。

- (ViewControllerB *)viewControllerB
{
return [TyphoonDefinition withClass:[ViewControllerB class]
configuration:^(TyphoonDefinition *definition) {

[definition useInitializer:@selector(initWithNibName:bundle:)
parameters:^(TyphoonMethod *initializer) {

[initializer injectParameterWith:@"ViewControllerB"];
[initializer injectParameterWith:[NSBundle mainBundle]];
}];

[definition injectProperty:@selector(assembly) with:self];
}];
}

向 ViewControllerB 添加一个属性:

//Start with this, next task is to back this with a protocol
@property(nonatomic, strong) AppAssembly *assembly;

现在更改实例化 ViewControllerC 的按钮操作:

ViewControllerC *viewControllerC = self.assembly.viewControllerC
[self.navigationController pushViewController:viewControllerC animated:YES];

现在我的应用程序类依赖于我的 TyphoonAssembly,如果我不想要这个怎么办?

所有 Typhoon 程序集都可以由协议(protocol)支持,因此您的应用程序只能看到实例的提供者,而不是 Typhoon。如果您曾经希望从 Typhoon 迁移,只需提供该协议(protocol)的替代实现——您仍将拥有一个健壮的架构。

关于ios - 台风不注入(inject)属性(property)(无 Storyboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com