Loading a nib file inside a UIViewController

Isuru picture Isuru · Apr 5, 2013 · Viewed 12k times · Source

I'm experimenting on how to load nib files to a UIViewController.

I have created a separate nib file called Email. First I noticed that when the view controller is loaded, the initWithNibName method isn't getting called. So I called it from the viewDidLoad method manually like this,

[self initWithNibName:@"Email" bundle:[NSBundle mainBundle]];

It did not work. Also I got a warning saying Expression result unused.

And I searched on the internet and came across this article and implemented the loadView method as described like so,

- (void)loadView
{
    [super loadView];

    UINib *nib = [UINib nibWithNibName:@"Email" bundle:nil];
    [nib instantiateWithOwner:self options:nil];
}

The method gets called but still the view controller is empty!

Can anybody tell me what I'm overlooking here and how this can be done?

Thank you.

UPDATE:

First off, thanks for all the responses. However voromax and svena's answers suggest that I should remove segues and load nibs automatically which I'm not very fond of. Anil's answer works find and now I have one last hurdle to jump.

I have multiple nib files. Depending on the user's selection, it should load a specific nib. So what I tried was, put all the nibs inside the array like so,

- (void)loadView
{
    [super loadView];

    NSArray *nibs = [[NSArray alloc] initWithObjects:
                     [[NSBundle mainBundle] loadNibNamed:@"Facsimile" owner:self options:nil],
                     [[NSBundle mainBundle] loadNibNamed:@"Email" owner:self options:nil],
                     [[NSBundle mainBundle] loadNibNamed:@"Memorandum" owner:self options:nil],
                     [[NSBundle mainBundle] loadNibNamed:@"ProjectMemo" owner:self options:nil], nil];


    self.view = [nibs objectAtIndex:0];
}

and access it using its index like this self.view = [nibs objectAtIndex:1];. But it throws an error *Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM _setViewDelegate:]: unrecognized selector sent to instance 0xd56fd20'*

Why is this error coming up? If its not possible, I'm open to suggestions.

Thanks again. And sorry for kinda dragging this out a bit.

Answer

Anil Varghese picture Anil Varghese · Apr 5, 2013

Use the below code to load a view from the nib and use as the view controller's view

- (void)loadView
{ 
[super loadView];

NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"test" owner:self options:nil];
self.view = [nib objectAtIndex:0];
}  

Edit

Load a single nib according to the user selection. See loading of a single nib

    NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"test" owner:self options:nil];  

will returns an array of objects. From your updated question I can see you are storing these arrays into another array. Now your nib array is an array of 'array' objects.

 self.view = [[nibs objectAtIndex:0]objectAtIndex:0]; 

will work.

But it is not good, load a single nib according to the user choise