在Main中的UIApplicationMain之后,Objective C的执行顺序是什么?

时间:2022-04-06 01:12:33

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?

有人可以解释如何控制iOS应用程序中的执行流程吗?我知道UIApplicationMain首先从main调用。那又怎样?我定义的方法和主要方法之间有什么关系?这一切都是事件驱动的还是可以有一些结构化的序列?

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:

例如,在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;
}

Thanks in advance.

提前致谢。

3 个解决方案

#1


7  

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.

正如您所说,UIApplicationMain在系统中创建应用程序执行。在应用程序加载过程中所做的事情中,我假设您对与特定应用程序相关的内容感兴趣。我还假设一个典型案例,Xcode提供的许多项目模板都说明了这种情况。

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.

应用程序加载过程会查看应用程序的信息属性列表。在那里找到“主nib文件基本名称”,应用程序的UIApplication实例从应用程序包中加载相应的nib文件。此nib文件指定应用程序委托类,并告知将类的实例连接到UIApplication实例的delegate属性。

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.

根据主nib文件,也可以创建和连接其他对象,例如,应用程序的窗口,主视图控制器等。

Now the loading sequence ends, and everything is all event-driven, starting from your application delegate class to get the famous -applicationDidFinishLaunching: message.

现在加载序列结束了,一切都是事件驱动的,从你的应用程序委托类开始,得到着名的-applicationDidFinishLaunching:消息。

#2


17  

So, as you mentioned, the main() function in main.m is the starting point, which then calls UIApplicationMain(). If you check the docs, you'll see that UIApplicationMain takes four arguments:

因此,正如您所提到的,main.m中的main()函数是起点,然后调用UIApplicationMain()。如果你检查文档,你会看到UIApplicationMain有四个参数:

  • argc,
  • *argv[],
  • *principalClassName
  • *delegateClassName.

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 be UIApplication. Unless you intend on subclassing UIApplication, you specify nil for the third argument. The fourth argument specifies which class should be UIApplication's delegate class, which will respond to anything specified in the UIApplicationDelegate Protocol. You don't have to muck with this directly, as it's included in all of the Xcode templates:

前两个只是从main()传递的参数count和变量列表。但第三和第四个参数是指向NSStrings的指针。第三个参数指定哪个类应该是UIApplication。除非您打算继承UIApplication,否则为第三个参数指定nil。第四个参数指定哪个类应该是UIApplication的委托类,它将响应UIApplicationDelegate协议中指定的任何内容。您不必直接使用它,因为它包含在所有Xcode模板中:

int main(int argc, char *argv[])
{
    @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

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 of AppDelegate.m.

不要让NSStringFromClass([AppDelegate class]))咬你。这只是指定第四个参数的一种奇特方式,以便在您稍后更改AppDelegate.m的名称时调用正确的委托。

UIApplication starts the main event loop and calls -application:didFinishLaunchingWithOptions:, one of the methods its delegate has to handle. Take a look at AppDelegate.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 the UIWindow and other instances of UIView get created:

UIApplication启动主事件循环并调用-application:didFinishLaunchingWithOptions:,它的委托必须处理的方法之一。看看AppDelegate.m,你会发现这个方法的一些模板代码。在这里您可以开始自定义,创建UIWindow和其他UIView实例创建之前需要的东西:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

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/

所有这一切,以及更多,都在这里得到了极好的解释:http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/

#3


2  

From Apple Documents -

来自Apple文件 -

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.

应用程序生命周期构成应用程序启动和终止之间发生的事件序列。在iOS中,用户通过点击主屏幕上的图标来启动您的应用程序。点击发生后不久,系统会显示一些过渡图形,然后通过调用其主要功能继续启动应用程序。从现在开始,大部分初始化工作将移交给UIKit,UIKit将加载应用程序的主nib文件并准备事件循环。

Application Life Cycle

应用生命周期

#1


7  

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.

正如您所说,UIApplicationMain在系统中创建应用程序执行。在应用程序加载过程中所做的事情中,我假设您对与特定应用程序相关的内容感兴趣。我还假设一个典型案例,Xcode提供的许多项目模板都说明了这种情况。

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.

应用程序加载过程会查看应用程序的信息属性列表。在那里找到“主nib文件基本名称”,应用程序的UIApplication实例从应用程序包中加载相应的nib文件。此nib文件指定应用程序委托类,并告知将类的实例连接到UIApplication实例的delegate属性。

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.

根据主nib文件,也可以创建和连接其他对象,例如,应用程序的窗口,主视图控制器等。

Now the loading sequence ends, and everything is all event-driven, starting from your application delegate class to get the famous -applicationDidFinishLaunching: message.

现在加载序列结束了,一切都是事件驱动的,从你的应用程序委托类开始,得到着名的-applicationDidFinishLaunching:消息。

#2


17  

So, as you mentioned, the main() function in main.m is the starting point, which then calls UIApplicationMain(). If you check the docs, you'll see that UIApplicationMain takes four arguments:

因此,正如您所提到的,main.m中的main()函数是起点,然后调用UIApplicationMain()。如果你检查文档,你会看到UIApplicationMain有四个参数:

  • argc,
  • *argv[],
  • *principalClassName
  • *delegateClassName.

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 be UIApplication. Unless you intend on subclassing UIApplication, you specify nil for the third argument. The fourth argument specifies which class should be UIApplication's delegate class, which will respond to anything specified in the UIApplicationDelegate Protocol. You don't have to muck with this directly, as it's included in all of the Xcode templates:

前两个只是从main()传递的参数count和变量列表。但第三和第四个参数是指向NSStrings的指针。第三个参数指定哪个类应该是UIApplication。除非您打算继承UIApplication,否则为第三个参数指定nil。第四个参数指定哪个类应该是UIApplication的委托类,它将响应UIApplicationDelegate协议中指定的任何内容。您不必直接使用它,因为它包含在所有Xcode模板中:

int main(int argc, char *argv[])
{
    @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

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 of AppDelegate.m.

不要让NSStringFromClass([AppDelegate class]))咬你。这只是指定第四个参数的一种奇特方式,以便在您稍后更改AppDelegate.m的名称时调用正确的委托。

UIApplication starts the main event loop and calls -application:didFinishLaunchingWithOptions:, one of the methods its delegate has to handle. Take a look at AppDelegate.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 the UIWindow and other instances of UIView get created:

UIApplication启动主事件循环并调用-application:didFinishLaunchingWithOptions:,它的委托必须处理的方法之一。看看AppDelegate.m,你会发现这个方法的一些模板代码。在这里您可以开始自定义,创建UIWindow和其他UIView实例创建之前需要的东西:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

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/

所有这一切,以及更多,都在这里得到了极好的解释:http://oleb.net/blog/2012/02/app-launch-sequence-ios-revisited/

#3


2  

From Apple Documents -

来自Apple文件 -

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.

应用程序生命周期构成应用程序启动和终止之间发生的事件序列。在iOS中,用户通过点击主屏幕上的图标来启动您的应用程序。点击发生后不久,系统会显示一些过渡图形,然后通过调用其主要功能继续启动应用程序。从现在开始,大部分初始化工作将移交给UIKit,UIKit将加载应用程序的主nib文件并准备事件循环。

Application Life Cycle

应用生命周期