我无法使用 XIB 将属性注入到视图控制器中initWithNibName:bundle:
例子:
这是我的组装:
@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
我已经检查了样本,它们与这种方法不同。请你能告诉我我做错了什么吗?
谢谢。