带有 Nib 文件和继承的 UIViewController
我正在尝试做一些非常棘手的事情,但我仍然停留在某个点上。我正在尝试使用从另一个带有其他 Nib 文件的 UIViewController
继承的 Nib 文件来实例化 UIViewController
。
问题是当我实例化我的儿子 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 :
// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:@"MotherViewController"
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
它只初始化并显示母类及其 IB 对象。我明白为什么,但我开始认为不可能做我想做的事。有什么建议吗?
编辑:
我会像这样使用我的 SonViewController :
SonViewController *son = [[SonViewController alloc]
initWithNibName:@"SonViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:son animated:YES];
[son release];
它应该显示儿子和母亲 IB 对象...
问候,
KL94
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;
}
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;
}
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 ?
Edit:
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];
It should display son and mother IB Object...
Regards,
kl94
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,出于这个原因,您应该只在 init 方法中使用特定的 nib,而不是 initWithNibName:bundle:。
然后,要使用 MotherViewController 的默认笔尖,只需使用
[[MotherViewController alloc] init];
。作为替代方案,您可以出于这个原因在 MotherViewController 中定义不同的初始值设定项。
然后,使用私有类别接口告诉 SonViewController 该方法。
Normally, you should only use a specific nib in the init method, and not the initWithNibName:bundle:, for this reason.
Then, to use the default nib for MotherViewController, you just use
[[MotherViewController alloc] init];
.As an alternate, you could define a different initializer in MotherViewController for this reason.
Then, use a private category interface to tell SonViewController about this method.
我知道这是一个旧线程,但我刚刚发现了一篇令人难以置信的博客文章 这里。
本质上,您必须迭代父类的所有视图,并将它们作为子视图添加到子类中。以下是我在项目中实现它的方法:
addSuviewsFromSuperclass 方法不是我的编码。我必须完全感谢我上面提到的博文的作者。下载他的示例项目,您将在他的 JMViewController.m 中找到它。
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:
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.