从变量加载 NIB
我正在尝试根据从设置文件中获取的变量加载 NIB。这是代码:
//select the right nib name
NSString *nibVar = [nibs objectForKey:@"controller"];
// create the view controller from the selected nib name
UIViewController *aController = [[UIViewController alloc] initWithNibName:nibVar bundle:nil];
aController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:aController animated:YES];
[aController release];
不幸的是,这不起作用。
这里有什么想法吗?
谢谢
I'm trying to load a NIB based on a variable I get from my settings file. This is the code:
//select the right nib name
NSString *nibVar = [nibs objectForKey:@"controller"];
// create the view controller from the selected nib name
UIViewController *aController = [[UIViewController alloc] initWithNibName:nibVar bundle:nil];
aController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:aController animated:YES];
[aController release];
This unfortunately does not work.
Any ideas here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用任意 NIB 实例化“UIViewController”,您必须使用该类的 NIB 实例化“[无论您的自定义视图控制器类是什么]”。
它崩溃是因为它试图访问 UIViewController 中不存在的属性。
如果您想要执行这种动态视图控制器加载,则需要做更多的工作,并使用特殊的 Class 类方法,该方法允许您使用类名字符串来实例化对象,而不是硬编码。
像这样的东西:
You cannot instantiate "UIViewController" with arbitrary NIBs, you have to instantiate "[whatever your custom view controller class is]" with the NIB for that class.
It's crashing because it's trying to access properties that don't exist in UIViewController.
If you want to do this kind of dynamic view-controller loading, you need to do a bit more work, and use the special Class class method that lets you instantiate an object using a string for the class name, instead of hard-coded.
Sometehing like:
确保 NIB 名称正确且不包含 xib 扩展名。它还区分大小写。
Make sure the NIB name is correct and does not include the xib extension. It is also case sensitive.