一个窗口中的多个视图 [英] Multiple Views in one Window

查看:85
本文介绍了一个窗口中的多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于视图的应用程序,但我对加载我的视图感到有点困惑。我想在同一个窗口中同时加载四个不同的视图。我似乎无法弄清楚如何做到这一点。如果可能的话,我宁愿以编程方式而不是使用界面构建器来完成所有操作。

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.

我的4个视图是:一个UIView,一个UIWebView,一个UITableView和另一个带按钮的UIView。

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

谢谢提前帮助。

推荐答案

iPhone应用程序中的视图按层次排列 - 也就是说,每个视图都有一个父视图(除了根视图)。这里有趣的是UIWindow本身是UIView的子类,因此您可以直接将所有四个视图添加到窗口中。 (这可能不是最好的方法,但它可能是最简单的。)

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.)

你真正要做的就是初始化你的每一个以编程方式查看您希望它们在UIWindow中具有的位置和尺寸。您可以通过在init方法或之后(取决于视图类型)为每个视图提供 frame 参数来执行此操作。因此,例如,在您的app委托中,您可以添加以下代码:

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];

这将创建一个100x100像素的视图并将其添加到上层窗口的左角。您可以为其他三个视图执行类似的操作。

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.

请注意,开发人员通常不会直接在应用程序委托中初始化视图 - 更好的方法可能是第五个视图将该位置作为其他四个视图的根视图,然后将该根视图添加到窗口中。您可以使用第五个视图的视图控制器来简化此任务 - 将视图初始化代码移动到该视图控制器的实现中,然后从app委托中,您可以实例化视图控制器并让它从那里接管。

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.

这篇关于一个窗口中的多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆