从一个 UIViewController 管理多个 UIView

发布于 2024-09-26 02:23:54 字数 459 浏览 0 评论 0原文

我对视图控制器感到困惑,并且希望有一个直接的例子。这是序言:

我有一个带有匹配的 .xib 的 UIViewController。 默认情况下,IB 在文档窗口中提供单个视图。 我可以通过告诉我的 UIWindow addSubview:controller.viewbringSubviewToFront:controller.view 来让它出现,

这是问题:

  1. 我应该向 ViewController 添加另一个视图吗?在IB?或者有更好的编程方式吗?

  2. 如何告诉 ViewController 在视图之间切换?

  3. 从 ViewController 向下看,实现此目的的代码是什么样的?

我正在尝试一些事情,但只是把事情弄得一团糟,所以我想我应该停下来问一下......

I'm getting confused on view controllers and would love a straight example. Here's the preamble:

I have a UIViewController with a matching .xib.
By default IB gives me a single View in the Document window.
I can make it appear by telling my UIWindow to addSubview:controller.view and bringSubviewToFront:controller.view

Here's the questions:

  1. Should I add another View to the ViewController in IB? Or is there a better, programmatical way?

  2. How do I tell the ViewController to switch between the Views?

  3. From the ViewController downward, what does the code look like to achieve this?

I'm trying things but just making a mess so I thought I'd stop and ask...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏辞 2024-10-03 02:23:54

请注意,主视图控制器中的每个按钮、标签、图像等实际上本身就是一个视图,但是我将您的问题解释为您想要管理多个全屏视图或“屏幕”。每个屏幕都应该有自己的视图控制器来管理它。因此,为了正确理解术语,视图控制器是一个管理单个全屏视图的对象(或者,如果它嵌套在导航控制器或选项卡栏控制器中,则几乎是全屏视图),而视图是由视图控制器以及其中的所有子视图(图像、按钮、标签等)(它们都是 UIView 子类)。视图控制器管理该屏幕上的所有内容,如果您想要另一个屏幕/页面,那么您应该创建一个新的视图控制器来管理它。

根视图控制器(添加到窗口的那个)可以是您在 IB 中设计的普通旧的普通视图控制器,但是如果您使用导航控制器或选项卡栏控制器并添加您设计的视图,它可能会更有用控制器 - 然后您可以根据需要推送其他视图控制器。

另一种方法(如果您不想要导航或选项卡栏样式)是使用您喜欢的任何转换直接在主窗口中转换到其他视图控制器(或者只是替换旧的转换)。不过我们暂时先不谈这个。

主视图控制器的任何子视图(您在 IB 中设计的视图)都将从 nib 文件中自动加载,但如果需要,您也可以通过编程方式添加自己的视图(通常您会使用其中之一,即以编程方式使用笔尖,但如果需要,您可以混合搭配)。要以编程方式执行此操作,请在视图控制器中重写 loadView ,然后调用 [super loadView]; 然后执行 [self.view addSubView:myOtherView]; (当然首先创建myOtherView)。请注意,第一次在视图控制器上访问 .view 时,它实际上调用 loadView 来创建视图,因此在 loadView 内部,重要的是在尝试访问 self.view 之前调用 [super loadView]; :D

要在视图之间切换,使用导航或选项卡栏控制器非常容易。因此,将主视图控制器放入(例如)导航控制器中,并将导航控制器放入窗口中,这样就得到了 window->navigationController->myController。然后,从视图控制器中的操作方法(您可以连接 IB 中的操作方法),例如,当按下“关于”按钮时,执行以下操作:

- (void)doAbout
{
    // Create the about view controller
    AboutViewController* aboutVC = [AboutViewController new];
    // Push the view controller onto the navigation stack
    [self.navigationController pushViewController:aboutVC animated:YES];
    [aboutVC release];
}

请注意,关于视图控制器是在此处以编程方式创建的 - 如果您的关于视图是在 IB 中设计,然后使用 initWithNibName:bundle: 来创建它。

这就是管理多个屏幕的方式。

Note that every button, label, image, etc. in your main view controller is actually a view in itself, however I've interpreted your question to mean that you want to manage multiple full-screen views or "screens". Each screen should have its own view controller to manage it. So to get the terminology right, a view-controller is an object that manages a single full-screen view (or almost full screen if it's nested inside a navigation controller or tab bar controller for example) and a view is the big area managed by the view controller as well as all the sub-views (images, buttons, labels, etc.) within it (they are all UIView sub-classes). The view controller manages all of them on that screen, if you want another screen/page then you should create a new view controller to manage it.

The root view controller (the one you add to the window) can be a plain old normal view controller that you've designed in IB, however it's probably more useful if you use a navigation controller or a tab bar controller and add your designed view controller to that - then you can push additional view controllers as needed.

Another way (if you don't want navigation or tab-bar style) would be to transition to other view controllers directly in the main window using whatever transitions you like (or just replace the old one). We'll leave that for now though.

Any sub-views of your main view controller (the one you've designed in IB) will be automatically loaded from the nib file, but you can also add your own views programatically if you want (typically you would use one or the other, i.e. nibs or programatically, but you can mix and match if you want). To do it programatically, override loadView in the view controller and then call [super loadView]; then do [self.view addSubView:myOtherView]; (create the myOtherView first of course). Note that the first time .view is accessed on your view controller, it actually calls loadView to create the view, so inside loadView it's important to call [super loadView]; before trying to access self.view :D

To switch between views, using the navigation or tab bar controllers makes it very easy. So put your main view controller inside (for example) a navigation controller and put the navigation controller in the window, so you've got window->navigationController->myController. Then from an action method in your view controller (you can hook up the action methods in IB), for example when an "about" button is pressed do this:

- (void)doAbout
{
    // Create the about view controller
    AboutViewController* aboutVC = [AboutViewController new];
    // Push the view controller onto the navigation stack
    [self.navigationController pushViewController:aboutVC animated:YES];
    [aboutVC release];
}

Note that the about view controller is created programatically here - if your about view is designed in IB then instead use initWithNibName:bundle: to create it.

And that's how you manage multiple screens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文