The Swift Code之应用程序的启动过程

前端之家收集整理的这篇文章主要介绍了The Swift Code之应用程序的启动过程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

The Swift Code主要是通过编写代码来完成应用程序的实现,使我们更能够深入的了解其编程语言实现的原理.也能够辅助更快的使用XCODE开发应用程序.

这篇文章主要是讲解启动应用程序从代码入口.

其实这里主要是通过注解来实现的,新建IOS swift项目的时候,会生成一个AppDelegate文件,这个文件就是应用程序的代码入口,在声明类的同时加入了注解@UIApplicationMain,表明这个应用程序.其实在这之前,我们必须在配置文件里设置启动入口为Main

4801DAAE-B3CF-4446-B37D-97761FC4D9BC.png

以下讲解,我们在代码里做讲解,大家可以试试在模拟里调试

  1. importUIKit
  2.  
  3. @UIApplicationMain
  4. classAppDelegate:UIResponder,UIApplicationDelegate{
  5.  
  6. varwindow:UIWindow?
  7.  
  8. /*程序未启动时,点击应用程序触发该方法,之后触发applicationDidBecomeActive*/
  9. funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[NSObject:AnyObject]?)->Bool{
  10. //Overridepointforcustomizationafterapplicationlaunch.
  11.  
  12. NSLog("开始启动application........")
  13.  
  14. window=UIWindow(frame:UIScreen.mainScreen().bounds)
  15. window?.backgroundColor=UIColor.orangeColor()
  16.  
  17.  
  18. window?.rootViewController=ViewController()
  19.  
  20.  
  21. returntrue
  22. }
  23.  
  24. /*点击HOme键后,程序即将进入委托处理(委托给系统处理),紧跟着将会触发applicationDidEnterBackground方法*/
  25. funcapplicationWillResignActive(application:UIApplication){
  26. //Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
  27. //UsethismethodtopauSEOngoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.
  28. NSLog("开始启动applicationWillResignActive........")
  29. }
  30. /*用户点击了Home按键,程序进入后台处理*/
  31. funcapplicationDidEnterBackground(application:UIApplication){
  32. //Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.
  33. //Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.
  34. NSLog("开始启动applicationDidEnterBackground........")
  35. }
  36.  
  37. /*在应用程序未消亡状态,状态由后台处理,进入前台处理,触发该方法,之后触发applicationDidBecomeActive*/
  38. funcapplicationWillEnterForeground(application:UIApplication){
  39. //Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.
  40. NSLog("开始启动applicationWillEnterForeground........")
  41. }
  42. /*应用程序进入后,触发该方法*/
  43. funcapplicationDidBecomeActive(application:UIApplication){
  44. //Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.IftheapplicationwasprevIoUslyinthebackground,optionallyrefreshtheuserinterface.
  45. NSLog("开始启动applicationDidBecomeActive........")
  46. }
  47.  
  48. funcapplicationWillTerminate(application:UIApplication){
  49. //Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.
  50. NSLog("开始启动applicationWillTerminate........")
  51. }
  52.  
  53.  
  54. }


转载至吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=87

猜你在找的Swift相关文章