从一个UIViewController管理多个UIView [英] Managing multiple UIViews from one UIViewController

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

问题描述

我对视觉控制器感到困惑,并且会喜欢一个直接的例子。这是序言:

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

我有一个匹配.xib的UIViewController。
默认情况下,IB在文档窗口中为我提供单个视图。
我可以告诉我的UIWindow addSubview:controller.view bringSubviewToFront:controller.view

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

以下是问题:


  1. 我应该添加另一个视图到IB的ViewController?或者是否有更好的,程序化的方式?

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

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

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

从ViewController向下,代码看起来是什么样的?

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

我正在尝试但只是弄得一团糟所以我想我会停下来问...... / p>

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

推荐答案

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

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.

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

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.

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

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

要在视图之间切换,使用导航或标签栏控制器可以非常轻松。因此,将主视图控制器放在(例如)导航控制器中并将导航控制器放在窗口中,这样你就有了window-> navigationController-> myController。然后从视图控制器中的动作方法(您可以在IB中连接动作方法),例如当按下关于按钮时执行以下操作:

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

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

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.

这篇关于从一个UIViewController管理多个UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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