UIViewController与Nib文件和继承 [英] UIViewController with Nib File and Inheritance
问题描述
我正在尝试做一些非常棘手的事情,而且我仍然陷入困境。我正在尝试使用从其他 UIViewController
继承的Nib文件和其他Nib文件来实例 UIViewController
。 br>
问题是我实例化我的儿子 UIViewController
。
I'm trying to do something really tricky and I'm still stuck at a point. I'm attempting to instance an UIViewController
with a Nib file inherited from an other UIViewController
with an other Nib file.
The problem is when I instantiate my son UIViewController
.
// SonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
init方法 initWithNibName:bundle:
应该调用超类
,但它只调用自己的nib文件。在超类中,我试图覆盖 initWithNibName:bundle:
方法并自己放置nibName:
The init method initWithNibName:bundle:
should call the super class
but it only call its own nib file. In the super class, I tried to override the initWithNibName:bundle:
method and put the nibName myself like that :
// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:@"MotherViewController"
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
它只是初始化并显示 Mother Class
及其IB对象。我理解为什么,但我开始认为不可能做我想做的事。有什么建议吗?
It only init and display the Mother Class
and its IB Object. I understand why but I begin thinking it is impossible to do what I want. Any suggestion ?
编辑:
我会像这样使用我的SonViewController:
I would use my SonViewController just like that :
SonViewController *son = [[SonViewController alloc]
initWithNibName:@"SonViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:son animated:YES];
[son release];
它应显示儿子和母亲IB对象......
It should display son and mother IB Object...
问候,
kl94
Regards,
kl94
推荐答案
我知道这是一个老线程,但我刚刚发现了一篇令人难以置信的博客文章这里。
I know this is an old thread, but I just found an incredibly blog post here.
基本上,您必须遍历父类的所有视图,并将它们作为子视图添加到子类中。以下是我在项目中实现它的方法:
Essentially, you have to iterate through all the views of the parent class and add them as subviews to your child class. Here's how I implemented it in my project:
// ChildViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self addSubviewsFromSuperclass];
}
// ParentViewController.h
- (void)addSubviewsFromSuperclass;
// ParentViewController.m
- (void)addSubviewsFromSuperclass
{
UIView *selfView = self.view;
UIView *nibView = nil;
@try
{
nibView = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self superclass]) owner:self options:nil][0];
}
@catch (NSException *exception)
{
NSLog(@"Something exceptional happened while loading nib:\n%@", exception);
}
self.view = selfView;
for (UIView *view in nibView.subviews)
{
[self.view addSubview:view];
}
}
addSuviewsFromSuperclass 方法是不是我的编码。我必须完全赞赏我上面提到的博客文章的作者。下载他的示例项目,你会在他的JMViewController.m中找到它。
That addSuviewsFromSuperclass method is not my coding. I have to give full credit to the author of the blogpost I mentioned above. Download his example project and you'll find it in his JMViewController.m.
这篇关于UIViewController与Nib文件和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!