iOS 中的视图管理

发布于 2024-10-16 00:28:37 字数 466 浏览 1 评论 0原文

我已经创建了几个 iPhone 和 iPad 应用程序,但我是使用另一个支持 api 或使用支持场景管理和屏幕转换的开源库(如 Cocos2d)编写它们。我觉得我还没有完全弄清楚如果没有这些外部 API,屏幕/场景/视图管理应该如何完成。有人可以非常简单但清楚地解释应用程序如何管理屏幕转换和/或向我指出一个很好的参考资料,以尽可能少的混乱来演示这一点吗?

我是否应该使用 UIViewController 派生类来分离应用程序的逐屏功能(除了已经用于处理多屏操作(如导航控制器)的功能之外)?我的应用程序本质上会将控制权移交给这些视图控制器之一,然后该控制器将使用其组件(如标签、按钮、任何视图)构建我的场景。我如何切换到相关的视图控制器,我是否应该期望 loadView 被调用?然后,当某个触发器调用移动到屏幕 B 时,是否会调用“转出”我的视图,然后调用其他一些视图控制器来“设置您的视图”并将它们转入?

如果有一个示例程序能够真正说明这一点,最好没有界面生成器,那将是理想的。

I've already created a couple iPhone and iPad apps, but I've written them either with another supporting api or with open source libraries like Cocos2d underneath supporting the scene management and screen transitions. I don't feel like I've quite figured out how screen/scene/view management is supposed to be done without these external apis. Can someone very simply, but clearly explain how an app would manage screen transitions and/or point me to a good reference that demonstrates this with as little clutter as possible?

Should I be using UIViewController-derived classes to separate the screen-by-screen functionality of my app (except for functionality that is already meant to handle multi-screen actions like navigation controllers)? And would my app essentially hand control over to one of these view controllers which would then construct my scene with its components (like labels, buttons, whatever views). How do I switch to the relevant view controller, and should I expect loadView to be called as a result? Then, when some trigger calls for moving to, say, screen B, would a call be made to 'transition out' my views, then call forth some other view controller to 'set up your views' and transition them in?

If there is a sample program that really illustrates this, preferably without interface builder, that would be ideal.

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

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

发布评论

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

评论(2

走走停停 2024-10-23 00:28:37

我应该使用
UIViewController 派生类
将屏幕与屏幕分开
我的应用程序的功能?

是的。

我的应用程序本质上会手
控制这些视图之一
然后将构造的控制器
我的场景及其组件?

是的,尽管许多 VC 只是从 nib 获取视图树,并关注核心控制器职责: 1. 将模型更改传播到各个视图字段; 2. 将视图事件传播回模型上的适当操作。我建议掌握 InterfaceBuilder 的窍门。它不完美且令人烦恼,但它可以节省您的工作时间和大量代码。

如何切换到相关的视图控制器?

有多种方法:

  • 如果您的 VC 托管在导航控制器中,则 [[self navigationController] PushViewController:theNextVCAnimated:YES] 是一种常见的习惯用法。
  • 在一组 VC 之间进行切换(其中没有明确的层次结构)最好使用 UITabBarController 来完成。
  • 如果你想进行模式堆叠,[selfpresentModalViewController:theNextVCanimated:YES]就是这样。不过,模态堆栈深度超过一两个就会变得笨拙。
  • 在 4.x 中,通过将第一个 VC 分配给窗口的 rootViewController 属性来呈现第一个 VC(应用程序委托可以执行此操作,或者主笔尖也可以);或者对于 4 之前的版本,通常在应用程序委托中找到传统方式:[myMainWindow addSubview:[myInitialVC view]]
  • 苹果提供的一些专门的 VC(视频、照片选择器)有自己的演示方法。

请注意,对于前两种方法,如果您想要行为而不是 UI 小部件,则可以隐藏导航栏或选项卡栏并以编程方式执行所有操作。

我应该期望 loadView 被调用吗?

会的,但是你大部分的“我现在就出现了!”逻辑属于 viewWillAppear: (在过渡动画开始之前调用,当视图还没有父级时)和 viewDidAppear: (在动画之后调用,当视图完全可见时) .) loadView 在您的 VC 认为很快需要视图时调用一次,并且不会再次调用,除非内存不足情况转储视图并且需要重新创建。

当您想用其他视图树构造方法完全替换 nib 加载时,请覆盖 loadView 。我开始这样做,但现在很少这样做了。

当您仍然希望进行正常的笔尖加载,但也希望自己进行一些视图树构建/后处理时,请重写 viewDidLoad 。我发现自己 99% 的时间都在这么做。

当某些触发器调用移动到屏幕 B 时,是否会调用“转出”我的视图,然后调用其他一些视图控制器来“设置您的视图”并将它们转入?

如果您使用前面列出的任何演示方法,就会发生转换。您需要做的就是实现 viewWill/DidAppear 和朋友,以便在发生时收到通知。

您可能希望研究“TheElements”示例项目(在 xcode 文档中搜索“the elements”)。根据要求,它不使用 nib;它以困难的方式设置代码中的所有内容。它有使用 TabBarController 和 NavigationController 的很好的例子,并以或多或少推荐的方式在 VC 之间划分责任。

Should I be using
UIViewController-derived classes to
separate the screen-by-screen
functionality of my app?

Yes.

And would my app essentially hand
control over to one of these view
controllers which would then construct
my scene with its components?

Yes, although many VCs just get their view tree from a nib, and concern themselves with the core controller responsibilities: 1. propagating model changes into the various view fields; and 2. propagating view events back into appropriate actions on the model. I recommend getting the hang of InterfaceBuilder. It's imperfect and annoying but it can save you hours of work and reams of code.

How do I switch to the relevant view controller?

A number of ways:

  • If your VCs are hosted in a navigation controller, [[self navigationController] pushViewController:theNextVC animated:YES] is a common idiom.
  • Switching among a set of VCs, where there's no clear hierarchy among them, is best done with a UITabBarController.
  • If you want to do modal stacking, [self presentModalViewController:theNextVC animated:YES] is how. Modal stacks deeper than one or two get unwieldy though.
  • Presenting the very first VC is done in 4.x by assigning it to your window's rootViewController property (the app delegate can do this, or the main nib can); or for pre-4, the traditional way usually found in the app delegate: [myMainWindow addSubview:[myInitialVC view]].
  • a few specialized apple-provided VCs (video, photo picker) have their own presentation methods.

Note that with the first two methods, if you want the behavior but not the UI widgets, you can hide the navigation bar or tab bar and do everything programmatically.

should I expect loadView to be called as a result?

It will, but most of your "I'm appearing now!" logic belongs in viewWillAppear: (called before the transition animation starts, when the view has no parent yet) and viewDidAppear: (called after the animation, when the view is fully visible.) loadView is called once when your VC thinks the view will shortly be needed, and isn't called again unless a low-memory condition dumps the view and it needs recreating.

Override loadView when you want to completely replace nib loading with some other view tree construction method. I started out doing this, but rarely do so anymore.

Override viewDidLoad when you still want the normal nib loading to happen, but also want to do some view tree construction/post-processing of your own. This is what I find myself doing 99% of the time.

when some trigger calls for moving to, say, screen B, would a call be made to 'transition out' my views, then call forth some other view controller to 'set up your views' and transition them in?

The transitioning happens for you, if you use any of the presentation methods listed earlier. All you need to do is implement viewWill/DidAppear and friends, to be informed when it happens.

You may wish to investigate the "TheElements" sample project (search the xcode docs for "the elements") As requested, it doesn't use nibs; it sets up everything in code the hard way. It has good examples of using TabBarController and NavigationController, and splits responsibility among VCs in more or less the recommended way.

猫性小仙女 2024-10-23 00:28:37

您的基本控制器有 2 个基本选项:UITabBarController 或 UINavigationController。 (您可以将 UINavigationController 放入 UITabBarController 中,但反之则不然)。

如果您有 UITabBarController,最简单的方法就是为每个选项卡分配不同的视图控制器(UIViewController 子类)。 UITabBarController 将处理它们之间的转换并调用 viewWillAppear 等方法。

使用 UINavigationController,您可以设置 rootViewController。然后,该视图控制器负责调用 [self.navigationController PushViewController:animated:]。这通常是通过 UITableViewController 完成的。 UINavigationController 将调用 viewWillAppear 等方法。

呈现视图控制器的最后一个选项是以模态方式呈现它。这就是弹出屏幕以填充屏幕的地方。您可以将其设置为从底部出现、翻转或淡入。为此,请创建视图控制器并调用 -[UIViewController PresentModalViewController:animated:]

除此之外,大多数视图控制器一次在屏幕上显示一个(iPad 上除外)。使用子类 UITableView 和 UIScrollView 或仅使用 UITableView。每个视图控制器应该独立于它的容器。这意味着您可以在 UINavigationController 上推送相同的视图控制器,或者将其设置为 UITabBarController 上的选项卡,或者以模态方式呈现它。

You have 2 basic options for your base controller, UITabBarController or UINavigationController. (You can put a UINavigationController inside a UITabBarController, but not the other way around).

If you have a UITabBarController, the easiest thing to do is to assign different view controllers (UIViewController subclasses) to each tab. The UITabBarController will handle transitions between them and call methods like viewWillAppear.

With a UINavigationController, you set the rootViewController. That view controller is then responsible for calling [self.navigationController pushViewController:animated:]. This is often done with a UITableViewController. The UINavigationController will call methods like viewWillAppear.

The last option to present a view controller is to present it modally. That is where a screen pops up to fill the screen. You can set it to come up from the bottom, flip over, or fade in. To do that, create your view controller and call -[UIViewController presentModalViewController:animated:].

Other than that, most view controllers are one on a screen at a time (except on iPad). Use subclasses UITableView and UIScrollView or just UITableView. Each view controller should be independent of it's container. Meaning that you could push the same view controller on a UINavigationController or set it as a tab on a UITabBarController or present it modally.

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