Vòng đời của 1 View Controller được tính từ lúc nó nạp vào bộ nhớ RAM cho tới khi nó bị huỷ.
1. File ViewController.swift
Vòng đời 1 view controller theo thứ như sau:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Khi ma chuong trinh khoi tao
print("Run 1")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Khi chuong trinh se khoi tao
print("Run 2")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Khi chuong trinh da khoi tao
print("Run 3")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("Run 4")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("Run 5")
}
//Vong doi 1 View Controller
//1. ViewdidLoad
//2. ViewWillAppear
//3. viewDidAppear
//4. viewWillAppear
//5. viewDidAppear
2. File AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print("App Cycle Run 1")
//Chay dau tien truoc nhat
return true
}
3. File SceneDelegate.swift
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
//Duoc khoi chay khi man hinh xoa khoi he thong. Trong trang thai Kill App
print("App Cycle Run 6")
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
//Duoc goi khi app chay nen tu trang thai InActive sang Actibe
print("App Cycle Run 3")
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
//Duoc goi khi App chuyen trang thai tu Active sang InActive
print("App Cycle Run 4")
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
print("App Cycle Run 2")
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
print("App Cycle Run 5")
}
Kết quả thứ tự:
App Cycle Run 1
Run 1
Run 2
App Cycle Run 2
App Cycle Run 3
Run 3
App Cycle Run 4
App Cycle Run 5
App Cycle Run 2
App Cycle Run 6
Run 4
Run 5
Không có nhận xét nào:
Đăng nhận xét