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
Best Answer
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.