iOS 是由蘋果公司開發的移動操做系統 。蘋果公司最先於 2007 年 1 月 9 日的 Macworld 大會上公佈這個系統(最初叫 iPhone runs OS X)。該系統最初是設計給 iPhone 使用的(因此後來曾命名爲 iPhone OS),以後陸續套用到 iPod touch、iPad 以及 Apple TV 等產品上(因此在 WWDC 2010 上最終宣佈改名爲 iOS)。swift
2007-2020 每一年發佈一個新版本,最新版本 iOS 14。數組
iOS 使用 Xcode 工具進行開發。能夠在 App Store 搜索安裝,也能夠去 Apple 開發者網站下載安裝(本教程基於 Xcode 12)。安全
Swift 或者 Objective-C(本教程基於 Swift 5.x)。markdown
##App初始化流程網絡
@main
(iOS 14 之前是 @UIApplicationMain)。didFinishLaunchingWithOptions
啓動方法。SceneDelegate
。class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
}
func sceneDidBecomeActive(_ scene: UIScene) {
}
func sceneWillResignActive(_ scene: UIScene) {
}
func sceneWillEnterForeground(_ scene: UIScene) {
}
func sceneDidEnterBackground(_ scene: UIScene) {
}
}
複製代碼
Application Scene Manifest
,找到Main Storyboard file base name
設置的 Storyboard。ViewController.swift
之間的聯繫(一個界面與一個類文件關聯)。Main Storyboard file base name
和Application Scene Manifest
最裏層的Storyboard Name
。willConnectToSession
中純代碼初始化 UIWindow,並設置顯示的第一個控制器。window —> rootViewController —> UIViewController —> UIView
。iOS 開發中,一個界面就是一個 UIViewController(視圖控制器),界面上顯示的內容就是 UIView(視圖)。session
UIViewController 中默認有一個和屏幕同樣大的 UIView,UIViewController 管理着它的生命週期。全部放在界面上的 UI 控件都放在 UIViewController 的 UIView 之上,在 UIViewController 的代碼中能夠經過self.view
屬性獲取它。開發中的其餘 UIView(及其子類)都放在該view
上。app
viewDidLoad
:View 完成內存加載。viewWillAppear
:View 即將顯示。viewDidAppear
:View 徹底顯示。viewWillDisappear
:View 即將消失。viewDidDisappear
:View 完全消失。獲取屏幕大小框架
UIScreen.main.bounds
複製代碼
所在容器View.viewWithTag
方法拿到這個視圖。
- 寬度或者高度其實爲0。
注意代碼的書寫位置,每每有人因爲書寫的位置不對致使代碼報錯。函數
如何在代碼中獲取 Storyboard 中的自定義 UIView?目前有兩種方式:工具
有沒有更加友好、更加直觀、更加便捷、更加高效的方法呢?答案是確定的,那就是 @IBOutlet 與 @IBAction。
Storyboard 中的 UIViewController 與想拖拽的類進行了關聯。
- @IBAction:
unrecognized selector sent to instance
- @IBOutlet:
this class is not key value coding-compliant for the key XXX