Search code examples
iphoneobjective-ciosxcodexcode4

The right way to implement loadView?


I have a question regarding the implementation of loadView:

Right now, I have it like this:

- (void)loadView
{
    image = [UIImage imageNamed:@"plan.gif"];
    scrollView=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen]           applicationFrame]];
    imageView = [[UIImageView alloc] initWithFrame:
             CGRectMake(0, 0,scrollView.frame.size.width + 40, scrollView.frame.size.height)];
    imageView.image = image;
    [imageView setContentMode:UIViewContentModeCenter];
    scrollView.contentSize = image.size;
    scrollView.maximumZoomScale = 3.0;
    scrollView.bounces = NO;
    scrollView.delegate = self;

    // do any further configuration to the scroll view
    // add a view, or views, as a subview of the scroll view.
    [scrollView addSubview:imageView];
    // release scrollView as self.view retains it
    self.view=scrollView;
    [scrollView release];
}

I suppose some of it should be in viewDidLoad:?

Thanks in advance.


Solution

  • This seems fine to me.

    viewDidLoad is normally used as your hook after getting a view returned from IB. In this case you are essentially just doing the work of IB in code (setting up the view heirachy and configuring it).

    Therefore in this case I think splitting out the logic may be superfluous unless it makes your code more readable.