WARNING : This content was auto generated from a backup file, links and images may be broken.

Interface builder is a great tool provide with the xCode and I make good use of it when building iPhone projects if you are scared by this Tool please take the time to play or watch some of the many tutorials online. I started using during my first iPhone project and I have not looked back since. l usually build basic / functional user interfaces then handing it over to a proper designer / artist to change coder art into a work of beauty. It has really helped our development process as I can try and concentrate on product functionality and not worry so much about how it looks. Though there I must admit that there is always a bit of too and fro that must be done as lots of things it is not a one way street.

In the past I have split a project into several nib’s each one containing a one or more views and a single Controller object this permits functionality to be split and sectioned easily. Having multiple nibs also reduces the lightly hood of trashing others work and limit bugs to a particular area.

In the latest project I have had to take one step further where a nib can contain a single view this permits the same view higher-archy to be constructed multiple times (such as for a table view).

It’s so easy to use nib’s and decode / load them multiple times. The usual way to load a nib is using the view controller function initWithNibName:bundle: but in some cases you do not want to construct a view controller you really only want the view. Well the easies way round that is to load the nib using the NSBundle this will provide an array of constructed objects (UIView NSObjects etc). This is really simple as the code below demonstrates.

// -----------------------------------------------------
// Load the first view from the nib
// -----------------------------------------------------
 (UIView*) loadFirstViewFromNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSBundle * bundle = ( nibBundleOrNil )  nibBundleOrNil : [NSBundle mainBundle];
NSArray * nib = [bundle loadNibNamed:nibNameOrNil owner:self options:nil];

for ( NSObject * obj in nib )
{
if ( [obj isKindOfClass:[UIView class]] )
{
return (UIView*)obj;
}
}

return nil;
}
Note : You will still need to retain the returned object. Loading stuff from a nib in this way has great potential I use this method to load individual UINavigationControllers when I want to branch the navigation off and return to the same point in the previous navigation controller. You branch of like this using the presentModalViewController function and dismissModalViewControllerAnimated to return. The code snippet below shows the simple alteration need to load a navigation controller.
// -----------------------------------------------------
// Load the first navigation controller from the nib
// -----------------------------------------------------
 (UINavigationController*) loadNavigationControllerFromNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSBundle * bundle = ( nibBundleOrNil )  nibBundleOrNil : [NSBundle mainBundle];
NSArray * nib = [bundle loadNibNamed:nibNameOrNil owner:self options:nil];

for ( NSObject * obj in nib )
{
if ( [obj isKindOfClass:[UINavigationController class]] )
{
return (UINavigationController*)obj;
}
}

return nil;
}


Just some small code snips this time but as always please feel free to use and alter as you see fit. If you have any questions please leave a comment or get in touch by other means. Thanks.

Comments

Comments powered by Disqus