开发者

Multiple Views in one Window

开发者 https://www.devze.com 2022-12-09 00:23 出处:网络
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 th

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.


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.


You can use [self parentViewController] to get access to the parent UIView

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号