无法从标识符为“(null)”的捆绑包中加载nib引用的图像。

3

我有一个应用程序,可以使用户在运行时从应用程序中切换语言。一切正常,但是当切换到另一种语言时,为该语言加载本地化的Xib,但是该Xib内部没有任何图像。以下是我的代码,用于切换到另一种语言。

 NSString* str = [[[NSUserDefaults standardUserDefaults]valueForKey:@"AppleLanguages"]      objectAtIndex:0]; 
 NSBundle* bnd = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:str ofType:@"lproj" ]];



HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:bnd];
UINavigationController *localNavigationController = [[UINavigationController alloc] initWithRootViewController:homeVC];
localNavigationController.viewControllers = [NSMutableArray arrayWithObject:homeVC];
localNavigationController.navigationBarHidden = YES;

NSMutableArray *navs = [NSMutableArray arrayWithObject: localNavigationController];

tabBarController.viewControllers = navs;

self.window.rootViewController = tabBarController;

这里是Xcode调试中发生错误的地方。

Could not load the "sahdow.png" image referenced from a nib in the bundle with identifier "(null)" 

我很高兴能帮助解决这个问题,这让我花费了很多时间。
1个回答

2

你加载 nib 文件的方式有误,会将图片“shadow.png”也强制本地化。实际上,如果你将此图片本地化,就会看到错误消失了,但我仍建议避免这种方法。

iOS 可以自动处理设置中的语言更改,并在需要时加载适当的本地化 xib。我建议你避免在代码中显式加载本地化的 xib,而是通过以下方式实例化你的视图控制器,让 iOS 来处理:

HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

注意: 我的回答假设 "sahdow.png" 在你的nib文件中有意引用。如果不是这种情况,请查找nib文件中对该图像的引用并将其删除。

附注: 我刚刚发现另一篇文章提到了你正在使用的加载本地化nib的解决方案以及它所产生的副作用,例如需要将所有相关图像进行本地化。


正确的解决方案是仅为所有图像进行本地化。 - wod
强制本地化所有图像是没有意义的(事实上,每次添加新语言时,本地化所有图像会呈指数级增长)。最好的解决方案是只本地化你需要的内容。通过遵循我上面发布的指示,你可以做到这一点。nib是本地化的,因此你可以选择加载哪些图像。 - diegoreymendez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接