台风未注入财产(无故事板)

2023-11-30

我无法使用 XIB 将属性注入到视图控制器中initWithNibName:bundle:

Example:

这是我的装配:

@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

应用程序委托代码:

@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

我已经检查过样本,它们与这种方法不同。请你告诉我我做错了什么吗?

Thanks.


Typhoon 是一种依赖注入容器,这意味着它不会影响、混合或以其他方式影响您的课程。因此,要获取注入了依赖项的类的实例,我们得问台风。

为什么不用于故事板?

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

在其他地方,为了获取注入了依赖项的实例,我们使用程序集接口,并询问 Typhoon。

解决步骤:

在程序集中添加 ViewControllerB 的定义。 (在 Objective-C 中,如果您愿意,您还可以使用自动注入宏)。

- (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];
    }];
}

向 ViewController 添加一个属性:

//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];

现在我的应用程序类取决于我的 Typhoon Assembly,如果我不想要这个怎么办?

所有 Typhoon 程序集都可以由协议支持,以便您的应用程序只能看到实例的提供者,而不是 Typhoon。如果您希望从 Typhoon 迁移,只需提供该协议的替代实现 - 您仍然拥有一个强大的架构。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

台风未注入财产(无故事板) 的相关文章

随机推荐

  • 使用 AppCompat v23 和 API21 构建应用程序时 Android Studio 无法工作

    我使用的是 Android 1 3 版本5 0 1 版 SDK我废弃了它 因为每当我构建项目时它都会抛出错误 有谁可以详细告诉一下怎么做吗 Error Information Gradle tasks app assembleDebug a
  • 正则表达式匹配分数

    我正在尝试执行正则表达式来匹配分数 用户将输入一个分数 例如 1 4 1 1 2 10 2 等 我已经测试了这个正则表达式并且它有效 但问题是当我输入 10 2 20 5 30 3 40 53 等时它无法识别这些分数 这是我的正则表达式正如
  • 使用 ASP.net MVC 的后退按钮功能

    我有一个要求 需要我将用户重定向到其浏览历史记录的上一页 我正在使用 ASP net MVC 1 0 我不想使用 javascript 来实现这一点 有什么指点吗 您可以使用Request UrlReferrer属性来呈现链接 也许是这样的
  • 在 SQL Management Studio 2005 中搜索

    有没有办法在存储过程中搜索文本 例如 我想查明某个特定的表是否被任何存储过程引用 SELECT OBJECT SCHEMA NAME object id OBJECT NAME object id FROM sys sql modules
  • Eclipse:java.lang.OutOfMemoryError: PermGen 空间

    我的日食配置 Eclipse 最新版本 Kepler 我的日食配置文件 startup plugins org eclipse equinox launcher 1 3 0 v20130327 1440 jar launcher libra
  • 使用 [NSThread detachNewThreadSelector:toTarget:withObject:] 时如何设置自动释放池

    您好 我正在使用 NSThread detachNewThreadSelector toTarget withObject 并且出现大量内存泄漏 因为我没有为分离线程设置自动释放池 我只是想知道我实际上在哪里做这件事 是在我打电话之前吗 N
  • 收到“消息:h 为空”

    我最近在使用时遇到了一些以前从未见过的情况selenium 代码 非常简单明了 from selenium import webdriver driver webdriver Firefox driver get http www drug
  • Jquery - 如何在动态创建后添加事件

    我动态创建一个 div 然后将其附加到容器中 var str div class dimsdiv div fpdiv append str 现在我什至需要为这些 div 添加悬停 我尝试只引用它 但后来发现我需要考虑 on 我尝试过这个 之
  • 关于派生类初始化中的基类

    伙计们 我看到几个案例 例如 class Derived public Base public Derived Base 什么情况或者有什么原则我们应该在 Derived ctor 初始化列表中显式初始化 Base 谢谢 如果您想使用参数调
  • PHP 正则表达式确定相对或绝对路径

    我正在使用 cURL 来提取远程站点的内容 我需要检查所有 href 属性并确定它们是相对路径还是绝对路径 然后获取链接的值并将其路径设置为 href http www website com index php url ABSOLUTE
  • 在 django 中将多个查询集转换为 json

    我问了一个今天早些时候的相关问题 在本例中 我有 4 个查询集结果 action count Action objects filter complete False onhold False annotate action count C
  • 我正在开发一款需要播放背景音乐的游戏 我想知道是否有办法隐藏音乐的进度条 只显示带有音量滑块的播放和停止按钮 现在我已经调整了宽度 以便仅显示播放按钮 但是当我将其更改为更大的宽度以显示滑块时 进度条又回来了 我想完全摆脱进度条 不幸的是
  • Sklearn One Hot Encoding 生成非表格输出

    我有一个这样的数据集 Entity Year Mean 0 Afghanistan 2016 0 99 1 Africa 2016 0 99 2 Albania 2016 0 99 3 Algeria 2016 0 99 4 America
  • 如何通过 Zend_Service_Amazon_S3 访问 Amazon s3 私有存储桶对象

    我在 Amazon S3 上创建了一个存储桶 并将一些图像保存在该存储桶中的一个文件夹中 所有图像都是私有的 我正在使用 Zend 的 Zend Service Amazon S3 类 如何访问私有图像 您可以通过像这样创建私有 url 来
  • 应用程序未在本机反应中注册

    I have a react native application and I changed the package name of the application using https www npmjs com package re
  • 为什么不在 C++ 中的所有内容都使用指针呢?

    假设我定义了一些类 class Pixel public Pixel x 0 y 0 int x int y 然后使用它编写一些代码 我为什么要执行以下操作 Pixel p p x 2 p y 5 来自 Java 世界的我总是这样写 Pix
  • 从另一个线程访问上下文

    我开始将一些 PC Java 应用程序迁移到 Android 环境 作为 Android 平台的完全新手 当我尝试使用时发现一个问题Service参考作为上下文Toast信息 这是我的相关部分Service code public clas
  • 我正在尝试使用 Firebase 为 Java 桌面应用程序构建后端

    几天来我一直在尝试使用 Java 不是 Android 和 Firebase 进行简单的用户身份验证 在开始使用 Firebase 的用户身份验证类之前 我尝试使用 Java 在 Firebase 参考上进行简单的保存和检索 但没有成功 没
  • 时间延迟重定向?

    我有一个网站 可以滑动到 HTML 中名为博客的 部分 我希望用户只需查看该页面一两秒 然后重定向到我的博客引擎 请建议延迟重定向 如果我可以根据博客李的点击延迟时间进行重定向 li class tile icon add favourit
  • 台风未注入财产(无故事板)

    我无法使用 XIB 将属性注入到视图控制器中initWithNibName bundle Example 这是我的装配 implementation AppAssembly ViewControllerC viewControllerC r