您的位置:首页 > 移动开发 > IOS开发

iOS开发之应用生命周期

2013-10-23 20:15 302 查看
在iphone程序中,main.m是入口文件,在main函数中又个入口函数:

    @autoreleasepool {

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AndyAppDelegate class]));

    }

参数说明:

argc和argv是ISO C标准的main函数的参数,直接传递给UIApplicationMain进行相关处理。

principalClassName是应用程序类的名字,该类必须继承自UIApplication类。

delegateClassName是应用程序类的代理类,该函数跟据delegateClassName创建一个delegate对象,并将UIApplication对象中的delegate属性设置为delegate对象

UIApplicationMain的作用:

1. 创建UIApplication对象

2.在类-info.plist中寻找由NSMainNib所定义的主窗口界面,将其装入内存并实例化其中的对象,如果没有这一项,则不会实例化。

每一个程序在运行期必须有且仅有一个UIApplication(或则其子类)的一个实例。创建UIApplication的单例实例。这样可以通过调用[UIApplication sharedApplication]来得到这个单例实例的指针。

UIApplication的一个主要工作是处理用户事件,它会起一个队列,把所有用户事件都放入队列,逐个处理,在处理的时候,它会发送当前事件 到一个合适的处理事件的目标控件。此外,UIApplication实例还维护一个在本应用中打开的window列表(UIWindow实例),这样它就 可以接触应用中的任何一个UIView对象。UIApplication实例会被赋予一个代理对象,以处理应用程序的生命周期事件(比如程序启动和关闭)、系统事件(比如来电、记事项警告)等等
以下是官方解释:
UIApplicationMain

This function is called in the main entry point to create the application object and the application delegate and set up the event cycle.

创建UIApplication单例,设置该单例的delegate,设置一个时间循环周期。
int UIApplicationMain (
   int argc,
   char *argv[],
   NSString *principalClassName,
   NSString *delegateClassName
);
Parameters
argc
The count of arguments in argv; this usually is the corresponding parameter to main.
argv
A variable list of arguments; this usually is the corresponding parameter to main.
principalClassName
The name of the UIApplication class or subclass. If you specify nil, UIApplication is assumed.
delegateClassName
The name of the class from which the application delegate is instantiated. If principalClassName designates a subclass of UIApplication, you may designate the subclass as
the delegate; the subclass instance receives the application-delegate messages. Specify nil if you load the delegate object from your application’s main nib file.
Return Value
Even though an integer return type is specified, this function never returns. When users exits an iOS application by pressing the Home button, the application moves to
the background.

更多内容可以参考:

点击打开链接

(出处: Android开发论坛 - 安卓开发论坛 - Android开发 - 安卓论坛 - 移动互联网门户)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发