Replacing a UIViewController root view with my own view
Asked Answered
A

2

7

I'm trying to replace a UIViewController root view with a subclassed UIView.

In the iPhone SDK's UIViewController Class Reference, within it's Overview section, this is stated:

You use each instance of UIViewController to manage a full-screen view. For a simple view controller, this entails managing the view hierarchy responsible for presenting your application content. A typical view hierarchy consists of a root view—a reference to which is available in the view property of this class—and one or more subviews presenting the actual content.

Following the link on the view property it then states:

If your view controller does not have an associated view controller, you should override the loadView method and use it to create the root view and all of its subviews.

If this code was placed in loadView would it create the root view assuming I was using my own subclass of UIView called MyView:

self.view = [MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

Thanks // :)

Antic answered 14/10, 2009 at 1:55 Comment(0)
C
6

You are correct. The loadView function is called when the view needs to be displayed, and the viewController's view property is the view that is displayed when using functions like presentModalViewController:animated:. If you are loading your view from a NIB instead of using loadView, then just change the class of the view to your custom subclass in the properties tab for that view.

Coryden answered 14/10, 2009 at 2:50 Comment(1)
The docs for loadView state the following: You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.Wargo
C
1

You should note that anything you set the view property to gets retained. So you should either use

self.view = [[[MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];

or

MyView *v = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = v;
[v release];

to balance that out.

Consumedly answered 14/10, 2009 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.