UIViewController with Nib file and overlay

I'm trying to do something really complicated, and I'm still looping. I am trying an instance UIViewControllerwith a Nib file inherited from another UIViewControllerwith a different Nib file.
The problem is that I create 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 super class, but it only calls its own nib file. In the superclass, I tried to override the method initWithNibName:bundle:and put the name nibName myself:

// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:@"MotherViewController" 
                                bundle:nibBundleOrNil])) {
        // Custom initialization.
    }
    return self;
}

It only initializes and displays Mother Classits IB object. I understand why, but I'm starting to think that it's impossible to do what I want. Any suggestion?

Edit:
I would use my SonViewController like this:

SonViewController *son = [[SonViewController alloc] 
                      initWithNibName:@"SonViewController" bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:son animated:YES];
[son release];

It should display the son and mother of IB Object ...

,
kl94

+5
2

, , .

, . :

// 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.

+1

, nib init, initWithNibName: bundle:, .

@implementation MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        //custom initialization
    }
    return self;
}
- (id)init {
    return [self initWithNibName:@"MotherViewController" bundle:nil];
}

, nib MotherViewController, [[MotherViewController alloc] init];.

MotherViewController .

@implementation MotherViewController
- (id)_initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        //custom initialization
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [self _initWithNibName:@"MotherViewController" bundle:nibBundleOrNil];
}

, SonViewController .

//SonViewController.m
@interface MotherViewController (PrivateInit)
- (id)_initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@end
@implementation SonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        //custom initialization
    }
    return self;
}
+3

All Articles