目錄:[Swift]Xcode實際操做html
本文將演示使用iOS模擬器,演示程序的生命週期。swift
在項目導航區,打開應用代理文件【AppDelegate.swift】app
應用代理文件時系統運行本應用的委託,裏面定義瞭如程序的進入與退出、設備方向旋轉等衆多全局方法。ide
1 import UIKit 2 3 @UIApplicationMain 4 class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 8 //把程序載入後須要執行的代碼,寫在程序完成加載的方法裏面,這是最經常使用的一個方法 9 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 10 // Override point for customization after application launch. 11 12 //當程序完成加載的過程後,在控制檯輸出一行文字 13 print(">>>>>>>>>>>>>>>>>>>>>>>> didFinishLaunchingWithOptions") 14 return true 15 } 16 17 //當程序將要進入非活動狀態時,調用此方法,在此期間,程序不會接受消息或事件 18 func applicationWillResignActive(_ application: UIApplication) { 19 // 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. 20 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 21 print(">>>>>>>>>>>>>>>>>>>>>>>> applicationWillResignActive") 22 } 23 24 //當程序被推送到後臺的時候,調用此方法 25 //若是要設置後臺繼續某些動做,則在這個方法裏面添加代碼便可 26 func applicationDidEnterBackground(_ application: UIApplication) { 27 // 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. 28 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 29 print(">>>>>>>>>>>>>>>>>>>>>>>> applicationDidEnterBackground") 30 } 31 32 //當程序從後臺,將要從新回到前臺的時候,調用此方法 33 func applicationWillEnterForeground(_ application: UIApplication) { 34 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 35 print(">>>>>>>>>>>>>>>>>>>>>>>> applicationWillEnterForeground") 36 } 37 38 //當程序進入活動狀態的時候,執行該方法 39 func applicationDidBecomeActive(_ application: UIApplication) { 40 // 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. 41 print(">>>>>>>>>>>>>>>>>>>>>>>> applicationDidBecomeActive") 42 } 43 44 //當程序將要退出時,調用該方法 45 //一般是用來保存數據,和一些退出前的清理工做 46 func applicationWillTerminate(_ application: UIApplication) { 47 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 print(">>>>>>>>>>>>>>>>>>>>>>>> applicationWillTerminate") 49 } 50 }
【Hardware】硬件->【Home】->返回模擬器的主界面。post
【Hardware】硬件->【Lock】->鎖定模擬器。ui
解鎖模擬器:this
方式一:雙擊【Home】spa
方式二:【Command】+【Shift】+兩下【H】代理