204,UIApplication与代理方法

来源:互联网 发布:数控立车车床怎么编程 编辑:程序博客网 时间:2024/06/04 20:03

  • app受到干扰时,比如一个来电或者锁屏会导致app进入后台甚至被终止
  • 会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理来处理这些系统事件
  • delegate可处理的事件包括:
  • 应用程序的生命周期事件(如程序启动和关闭)
  • 系统事件(如来电)
  • 内存警告

1,UIApplication的代理方法


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    return YES;

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}




  • main函数中执行了一个UIApplicationMain这个函数
  • int UIApplicationMain(int argc,char *argv[], NSString *principalClassName,NSString *delegateClassName);
  • argcargv:直接传递给UIApplicationMain进行相关处理即可
  • principalClassName:指定应用程序类名(app的象征),该类必须是UIApplication(或子类)。如果为nil,则用UIApplication类作为默认值
  • delegateClassName:指定应用程序的代理类,该类必须遵守UIApplicationDelegate协议


  • UIApplicationMain函数会根据principalClassName创建UIApplication对象,根据delegateClassName创建一个delegate对象,并将该delegate对象赋值给UIApplication对象中的delegate属性
  • 接着会建立应用程序的Main Runloop(事件循环),进行事件的处理(首先会在程序完毕后调用delegate对象的application:didFinishLaunchingWithOptions:方法)
  • 程序正常退出时UIApplicationMain函数才返回



0 0
原创粉丝点击