views:

515

answers:

1

Hi everyone,

I'm writing a view based app, but I'm a bit confused about loading my views. I'd like to have four different views loaded at the same time in the same window. I can't seem to figure out how to do this. I'd prefer to do everything programatically rather than with the interface builder if possible.

My 4 views are: a UIView, a UIWebView, a UITableView and another UIView with buttons.

Thanks in advance for the help.

+3  A: 

Views in an iPhone app are arranged hierarchically - that is, each view has a "parent" view (excepting the root view). The interesting bit here is that UIWindow is itself a subclass of UIView, so you can add all four views to your window directly. (This may not be the best approach, but it's perhaps the simplest.)

All you really have to do is initialize each of your four views programmatically with the location and dimensions you want them to have in the UIWindow. You do this by giving each view a frame parameter, either in the init method or afterwards (depending on the type of view). So, for example, in your app delegate you could add this code:

CGRect frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
[window addSubview:view];

This will create a 100x100-pixel view and add it to the upper left corner of the window. You can do similar things for each of the other three views.

Note that developers usually don't initialize views directly in the app delegate - a better approach might be to have a fifth view take the place as the root view for the other four, then add that root view to the window. You can use a view controller for the fifth view to make this task easier - move the view initialization code into that view controller's implementation, then from the app delegate you can just instantiate the view controller and let it take over from there.

Tim
Awesome, thanks! That worked. I noticed that I have to shift everything down or it gets cut off. I'm guessing that's because of the title bar.
Adam
And if you create a UIView subclass to encapsulate this stuff, its layoutSubviews method would be the place to do the work of positioning stuff. That will get called any time the view's dimensions change, etc.
Sixten Otto
@Adam: yup, the title bar takes up 20px and covers the window. This is one of the handy things about using a new UIView as your root rather than the window itself - you can use (0,0) as your visible coordinate origin, rather than (0,20).
Tim
@Tim: I'm taking your note and putting the views into one view controller. The question I have now, is how do I access the window from the viewcontroller? If I do [super window] I get a warning, so I'm guessing that's the wrong approach.
Adam
@Adam: do you really need to? Ideally, what you would do is: create the view controller with its view hierarchy in the app delegate, add the view controller's view to the window in the delegate, then all the view controller's views would only deal with other views in the controller (including the "root" view), not the window.
Tim