Wednesday, July 13, 2011

Do we really need "UIViewController"?

I studied the "Scratch Off Effect" code from http://buildingmyworld.wordpress.com/ and found a very interesting thing.

There's only a "main.h", an "AppDelegate", 2 "UIView" and - - - an "UIViewController" which is not been used at all. If you look at the blog, the author also mentioned "btw: there is a view controller called ScratchTiceketViewController. Just ignore it. It was my test view controller and I’m to lazy to remove it :) )"

So I inserted a few NSLog in each of the methods to show how/when each method been called, and removed that "ScratchTicketViewController" completely. The code still works! What the....

This is totally different from what I learned so far - so we can have a application that has no UIViewController at all, very interesting!

Next, I tried to add an UIView and an UIViewController to the project and compare the differences.

For UIView, as below it only has 3 methods, and one of them - "drawRect" disabled -- I remember seeing that name in previous Quartz 2D test project. I guess if you need any other methods, can still be triggered from the "initWithFrame" method.


@implementation UIView1

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end

However, for UIViewController it's a lot more complicate. As below there are a few extra ones: "loadView", "viewDidLoad", "viewDidUnload", "didReceiveMemoryWarning" and "shouldAutorotateToInterfaceOrientation". Which is definitely more complicate and powerful than the simple UIView above.

@implementation UIViewController1

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

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

I guess it all depends on the purpose of the code, if it's just a simple test project/application, which you don't care whether there's orientation change or memory warning or use NIB files, a simple UIView should be sufficient. For a more "normal", "fully functioning" project/application which is to be submitted to App Store, you would definitely still have to use UIViewController.

Ah! Feels good to learn new things everyday!

0 comments:

Post a Comment