Loading a Nib file, Programmatically (Objective-C)

If you want to load a Nib file at run time by simply allocating and initializing a View object, then you should take a rather strange approach to how you create your class files. Suppose you have a subclass of UIView called MyView and what you want is to allocate and initialize an instance of MyView but have MyView load its outlets and actions from a Nib file. Well, you will need to do two things:

  1. In Interface Builder, change the class name of your View object to MyView.
  2. Then you will have to override MyView’s initWithFrame method like so:

- (id)initWithFrame:(CGRect)paramFrame
{

NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MyView"
owner:nil
options:nil];

if ([arrayOfViews count] < 1){
[self release];
return nil;
}

MyView *newView = [[arrayOfViews objectAtIndex:0] retain];
[newView setFrame:paramFrame];

[self release];
self = newView;

return self;

 

}

Then you can go ahead and initialize your view like this:

MyView *myView = [[MyView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:myView];
[myView release];

And here an instance of MyView will get added to the view of a view controller. That simple. I hope it helps some of you out there 🙂

Loggin in iOS SDK – More Tips and Tricks and Magic Macros

This is the ultimate debug-killer! This is the third video in the series of videos that I’ve prepared, teaching iOS developers how to take advantage of some of the most awesome features and macros in the iOS SDK in order to completely replace having to debug their code, with the smooth and easy ride of logging.

In this video, you will learn how to print out logs to the console with the exact line number from where the logs are getting printed. You will also learn how to put all the knowledge you’ve learnt from this video and the previous 2 videos in the series, into 3 handy and magical macros that you can use in your iOS projects. Enjoy!

Logging in iOS SDK – Tips and Tricks – Explained Through Video

This is the second video in the series of videos that I’m preparing, demonstrating to iOS developers how they can take advantage of logging facilities provided in the iOS SDK to be able to find defects and issues in their apps just by looking at the logs, without the need to use a debugger at all.

In this video, I will teach you two very handy tricks that you can use with NSLog method in Objective-C…