Main中UIApplicationMain之后的Objective C执行顺序是什么?
有人可以解释一下 iOS 应用程序中的执行控制流程吗?我知道 UIApplicationMain 首先从 main 调用。然后呢?我定义的方法和main有什么关系?都是事件驱动的还是可以有某种结构化的顺序?
我并不是要如此含糊,我只是需要知道从哪里开始。也许我看待这个问题的方式是错误的。
例如,在 C++ 中我会做类似的事情:
#include "myMethods.h"
int main (int argc, char * const argv[]) {
Method1(); // Initialization
Method2(); // Opening views and options
Method3(); // Meat of the program
return 0;
}
提前致谢。
Could someone please explain how control of execution flows in an iOS application? I know that UIApplicationMain is called first from main. Then what? What is the relationship between my defined methods and main? Is it all event-driven or can there be some structured sequence?
I don't mean to be so vague, I just need to know where to start. Perhaps I'm looking at this in the wrong way.
For example, in C++ I would do something like:
#include "myMethods.h"
int main (int argc, char * const argv[]) {
Method1(); // Initialization
Method2(); // Opening views and options
Method3(); // Meat of the program
return 0;
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,正如您所提到的,
main.m
中的main()
函数是起点,然后调用UIApplicationMain()
。如果您检查文档,您会看到UIApplicationMain
采用四个参数:其中前两个只是从
main()
传递的参数计数和变量列表。但第三个和第四个参数是指向 NSStrings 的指针。第三个参数指定哪个类应该是UIApplication
。除非您打算子类化UIApplication
,否则请为第三个参数指定nil
。第四个参数指定哪个类应该是 UIApplication 的委托类,它将响应UIApplicationDelegate
协议。您不必直接处理它,因为它包含在所有 Xcode 模板中:不要让
NSStringFromClass([AppDelegate class]))
位困扰您。这只是指定第四个参数的一种奇特方式,以便在您稍后更改 AppDelegate.m 的名称时调用正确的委托。UIApplication
启动 主事件循环并调用-application:didFinishLaunchingWithOptions:
,这是其委托必须处理的方法之一。查看AppDelegate.m
,您会找到此方法的一些模板代码。您可以在此处开始自定义,创建在创建UIWindow
和UIView
的其他实例之前需要就位的内容:现在是应用程序窗口和根视图控制器已定义,并且应用程序已关闭并正在运行。
所有这些以及更多内容都在这里得到了很好的解释: http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/
So, as you mentioned, the
main()
function inmain.m
is the starting point, which then callsUIApplicationMain()
. If you check the docs, you'll see thatUIApplicationMain
takes four arguments:The first two of those are just the argument count and variable list passed from
main()
. But the third and fourth arguments are pointers to NSStrings. The third argument specifies which class should beUIApplication
. Unless you intend on subclassingUIApplication
, you specifynil
for the third argument. The fourth argument specifies which class should be UIApplication's delegate class, which will respond to anything specified in theUIApplicationDelegate
Protocol. You don't have to muck with this directly, as it's included in all of the Xcode templates:Don't let the
NSStringFromClass([AppDelegate class]))
bit throw you. That's just a fancy way of specifying the fourth argument so that the right delegate gets called should you later change the name ofAppDelegate.m
.UIApplication
starts the main event loop and calls-application:didFinishLaunchingWithOptions:
, one of the methods its delegate has to handle. Take a look atAppDelegate.m
, and you'll find some template code for this method. This is where you can start customizing, creating things that need to be in place before theUIWindow
and other instances ofUIView
get created:So, now the application window and root view controller are defined, and the app is off and running.
All of this, and quite a bit more, is excellently explained here: http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/
正如您所说, UIApplicationMain 在系统中创建应用程序执行。在应用程序加载过程所做的事情中,我假设您对与特定应用程序相关的内容感兴趣。我还假设了一个典型的案例,Xcode 提供的许多项目模板中都有说明。
应用程序加载过程查看应用程序的信息属性列表。它在那里找到“主 nib 文件基本名称”,并且应用程序的 UIApplication 实例从应用程序包中加载相应的 nib 文件。此 nib 文件指定一个应用程序委托类,并指示将该类的实例连接到 UIApplication 实例的委托属性。
根据主 nib 文件,还可以创建和连接其他对象,例如应用程序的窗口、主视图控制器等。
现在加载序列结束,一切都是事件驱动的,从应用程序委托开始类来获取著名的
-applicationDidFinishLaunching:
消息。As you said UIApplicationMain creates an application execution in the system. Among the stuff the application loading process does, I assume you are interested in what is relevant to a specific application. Also I assume a typical case, which is illustrated in many project templates that Xcode provides.
The application loading process looks into the application's information property list. There it finds 'Main nib file base name', and the UIApplication instance of your application loads the corresponding nib file from the application bundle. This nib file specifies an application delegate class, and tells to connect an instance of the class to the delegate property of your UIApplication instance.
Depending on the main nib file, other objects may be created and connected as well, for example, the application's window, the main view controller, etc.
Now the loading sequence ends, and everything is all event-driven, starting from your application delegate class to get the famous
-applicationDidFinishLaunching:
message.来自 Apple 文档 -
应用程序生命周期构成了应用程序启动和终止之间发生的事件序列。在 iOS 中,用户通过点击主屏幕上的图标来启动您的应用程序。点击发生后不久,系统会显示一些过渡图形,并通过调用其主函数来启动您的应用程序。从此时起,大部分初始化工作都交给 UIKit,它加载应用程序的主 nib 文件并准备好事件循环。
应用程序生命周期
From Apple Documents -
The application life cycle constitutes the sequence of events that occurs between the launch and termination of your application. In iOS, the user launches your application by tapping its icon on the Home screen. Shortly after the tap occurs, the system displays some transitional graphics and proceeds to launch your application by calling its main function. From this point on, the bulk of the initialization work is handed over to UIKit, which loads the application’s main nib file and readies the event loop.
Application Life Cycle