从变量加载NIB [英] Load NIB from variable

查看:127
本文介绍了从变量加载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];

不幸的是,这不起作用.

这里有什么想法吗?

谢谢

解决方案

您无法使用任意NIB实例化"UIViewController",而必须使用该类的NIB实例化"[无论您的自定义视图控制器类是什么]". /p>

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

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

体贴如:

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

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

解决方案

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

这篇关于从变量加载NIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆