xcode 我可能创建的 RootViewController、AppDelegate 和 View Controller 类之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3626737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:41:53  来源:igfitidea点击:

What's the difference between the RootViewController, AppDelegate and the View Controller classes that I may create?

iosiphonexcodecocoa-touch

提问by kumar

I am trying to learn programming for the iPhone and I keep seeing these files and I am not sure when is each file and content of those files referred to in the execution of a program built for the iPhone. I am trying to follow tutorials and tips available online but nowhere is there a point by point comparison or something like that. It would be great if any of you could list a few basic differences like when is each file referred and what should ideally go into each file and so on. Thanks for your time already.

我正在尝试为 iPhone 学习编程,我一直看到这些文件,但我不确定在为 iPhone 构建的程序的执行过程中,每个文件和这些文件的内容何时被引用。我正在尝试遵循在线提供的教程和技巧,但没有任何地方可以进行逐点比较或类似的东西。如果你们中的任何人都可以列出一些基本差异,例如每个文件何时被引用以及理想情况下应该将什么放入每个文件等等,那就太好了。感谢您的时间。

回答by Peter

In general, delegates can be thought of as event handlers. Accordingly, the AppDelegate is the main event handler for your entire application. It is told when the application has launched, when it will exit, when a Push notification comes in, when the app has gone into the background, etc. One of those events - applicationDidFinishLaunching - is typically responsible for creating the application's window and adding views to that window.

一般来说,委托可以被认为是事件处理程序。因此,AppDelegate 是整个应用程序的主要事件处理程序。它被告知应用程序何时启动、何时退出、何时推送通知进入、何时应用程序进入后台等。这些事件之一 - applicationDidFinishLaunching - 通常负责创建应用程序的窗口和添加视图到那个窗口。

In most applications, the view that is added to the window is actually controlled by a UIViewController. Each UIViewController is responsible for managing the appearance of one main view plus all of its subviews. For example, a UITableViewController is responsible for managing a UITableView (main view) and all of the UITableViewCells (subview) that are inserted into that UITableView. The UIViewController typically acts as a delegate (event handler) to the views it is responsible for. When a user taps a table view cell, a method in the UITableViewController is called. When the user swipes to delete a separate method is called.

在大多数应用程序中,添加到窗口的视图实际上是由 UIViewController 控制的。每个 UIViewController 负责管理一个主视图及其所有子视图的外观。例如,一个 UITableViewController 负责管理一个 UITableView(主视图)和所有插入到该 UITableView 中的 UITableViewCells(子视图)。UIViewController 通常充当它负责的视图的委托(事件处理程序)。当用户点击表格视图单元格时,会调用 UITableViewController 中的一个方法。当用户滑动删除一个单独的方法被调用。

A generic UIViewController provides the same basic functionality, but for custom views. For example, the UIViewController may be responsible for displaying a few text views and a button. The UIViewController would create its main view, the text views and the button view. The text views and button view would be added to the view controller's main view as subviews. The UIViewController would register itself as the delegate for events from the text view (for example learning when the user has finished editing the text in the text view). It would also register a method to handle a button press originating from the button it owned. When any of these registered events occur, methods on the UIViewController are called allowing you to take whatever action is needed.

通用 UIViewController 提供相同的基本功能,但用于自定义视图。例如, UIViewController 可能负责显示一些文本视图和一个按钮。UIViewController 将创建它的主视图、文本视图和按钮视图。文本视图和按钮视图将作为子视图添加到视图控制器的主视图中。UIViewController 会将自己注册为文本视图中事件的委托(例如,了解用户何时完成了文本视图中文本的编辑)。它还将注册一个方法来处理源自它拥有的按钮的按钮按下。当这些注册事件中的任何一个发生时,UIViewController 上的方法将被调用,允许您采取任何需要的操作。

The rootViewController is a specific type of view controller used with navigation controllers. If you want an application that has the typical iOS navigation view hierarchy, your AppDelegate would typically add a UINavigationController to the app's window. That UINavigationController is useless without actually having content to display. That is where the rootViewController comes into play. You are responsible for providing a view controller (such as the one described above) to act as the first view stored in the UINavigationController's stack of views. This view will be displayed when the app starts up and anytime that the user pops subsequent ViewControllers off of the UINavigationController's stack.

rootViewController 是一种与导航控制器一起使用的特定类型的视图控制器。如果您想要一个具有典型 iOS 导航视图层次结构的应用程序,您的 AppDelegate 通常会将 UINavigationController 添加到应用程序的窗口。UINavigationController 在没有实际显示内容的情况下是无用的。这就是 rootViewController 发挥作用的地方。您负责提供一个视图控制器(例如上面描述的那个)作为存储在 UINavigationController 的视图堆栈中的第一个视图。该视图将在应用程序启动时以及用户从 UINavigationController 的堆栈中弹出后续 ViewController 时显示。

Long winded I realize - but hope it helps.

我意识到啰嗦——但希望它有所帮助。