从变量加载 NIB

发布于 2024-10-25 08:32:48 字数 502 浏览 4 评论 0原文

我正在尝试根据从设置文件中获取的变量加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2024-11-01 08:32:48

您不能使用任意 NIB 实例化“UIViewController”,您必须使用该类的 NIB 实例化“[无论您的自定义视图控制器类是什么]”。

它崩溃是因为它试图访问 UIViewController 中不存在的属性。

如果您想要执行这种动态视图控制器加载,则需要做更多的工作,并使用特殊的 Class 类方法,该方法允许您使用类名字符串来实例化对象,而不是硬编码。

像这样的东西:

Class viewControllerClass = NSClassFromString( nibVar );
UIViewController* aController = (UIViewController*) [[viewControllerClass alloc] initWithNibName:nibVar bundle:nil];

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:

Class viewControllerClass = NSClassFromString( nibVar );
UIViewController* aController = (UIViewController*) [[viewControllerClass alloc] initWithNibName:nibVar bundle:nil];
灯下孤影 2024-11-01 08:32:48

确保 NIB 名称正确且不包含 xib 扩展名。它还区分大小写。

Make sure the NIB name is correct and does not include the xib extension. It is also case sensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文