当前位置: 首页 > news >正文

iOS 开发入门指南-HelloWorld

iOS 开发入门指南

目录

  1. 项目结构解析
  2. iOS vs Android 对比
  3. 核心语法科普
  4. AppDelegate 与 SceneDelegate
  5. 开发工具与快捷键
  6. 总结与建议

项目结构解析

HelloWorldApp 项目目录结构

HelloWorldApp/
├── HelloWorldApp/                    # 源代码目录
│   ├── AppDelegate.swift             # 应用程序委托
│   ├── SceneDelegate.swift           # 场景委托 (iOS 13+)
│   ├── Controllers/
│   │   └── ViewController.swift      # 视图控制器
│   ├── Assets.xcassets/              # 资源文件
│   ├── Base.lproj/
│   │   └── Main.storyboard          # 故事板文件
│   └── Info.plist                   # 应用配置文件
├── HelloWorldApp.xcodeproj/          # Xcode 项目文件
└── HelloWorldApp.xcworkspace/        # Xcode 工作空间文件

目录作用说明

目录/文件作用Android 对应
HelloWorldApp/源代码和资源文件app/src/main/
HelloWorldApp.xcodeproj/项目配置和构建设置.idea/ + build.gradle
HelloWorldApp.xcworkspace/工作空间配置(多项目管理)settings.gradle
Assets.xcassets/图片、颜色等资源res/drawable/
Info.plist应用元数据配置AndroidManifest.xml

iOS vs Android 对比

架构对比

概念iOS (Swift)Android (Kotlin/Java)
应用入口AppDelegateApplication
界面控制器ViewControllerActivity
布局方式代码布局 + Auto LayoutXML布局 + ConstraintLayout
生命周期管理viewDidLoad, viewWillAppearonCreate, onStart, onResume

项目管理对比

iOS 特点:

  • Xcode 隐藏技术细节,专注代码开发
  • .xcodeproj.xcworkspace 在 Finder 中可见,Xcode 中隐藏
  • 统一的开发环境和工具链

Android 特点:

  • 项目结构完全可见和可编辑
  • 多种 IDE 选择(Android Studio、IntelliJ IDEA)
  • Gradle 构建系统更加透明

核心语法科普

1. 应用入口:AppDelegate

@main  // 相当于Android的Application类
class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?  // 相当于Android的根Activityfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {// 相当于Android Application的onCreate()window = UIWindow(frame: UIScreen.main.bounds)window?.rootViewController = ViewController()  // 设置根控制器window?.makeKeyAndVisible()return true}
}

Android 对比:

class MyApplication : Application() {override fun onCreate() {super.onCreate()// 应用初始化}
}

2. 界面控制器:ViewController

类声明和继承
// iOS
class ViewController: UIViewController {// 相当于Android的Activity
}
// Android
class MainActivity : AppCompatActivity() {// iOS的ViewController等价物
}
UI组件声明 - 闭包 vs 传统初始化

iOS 使用闭包初始化UI组件:

private let helloLabel: UILabel = {let label = UILabel()  // 创建实例label.text = "Hello, World!"  // 设置属性label.font = UIFont.systemFont(ofSize: 32, weight: .bold)label.textAlignment = .centerlabel.textColor = .systemBluelabel.translatesAutoresizingMaskIntoConstraints = false  // 关闭自动布局转换return label  // 返回配置好的实例
}()  // 立即执行闭包

Android 对比:

// 方式一:XML中定义 + 代码引用
<TextViewandroid:id="@+id/helloLabel"android:text="Hello, World!"android:textSize="32sp"android:textAlignment="center"android:textColor="@color/blue" />// 在Activity中引用
private lateinit var helloLabel: TextView// 方式二:纯代码创建(类似iOS)
private val helloLabel: TextView by lazy {TextView(this).apply {text = "Hello, World!"textSize = 32ftextAlignment = TextView.TEXT_ALIGNMENT_CENTERsetTextColor(ContextCompat.getColor(context, R.color.blue))}
}
iOS vs Android 组件对比详解
特性iOS (UIKit)Android (View System)
组件创建闭包初始化 {}()XML声明 + findViewByIdby lazy
属性设置点语法 label.text = "..."XML属性或代码设置 setText("...")
布局系统Auto Layout 约束LinearLayout/ConstraintLayout
字体设置UIFont.systemFont()android:textSizesetTextSize()
颜色系统.systemBlue 系统颜色@color/blue 资源引用
内存管理ARC 自动管理GC 垃圾回收
常用UI组件对应关系
iOS组件Android组件功能说明
UILabelTextView文本显示
UIButtonButton按钮控件
UITextFieldEditText文本输入
UIImageViewImageView图片显示
UIScrollViewScrollView滚动容器
UITableViewRecyclerView列表视图
UICollectionViewRecyclerView (Grid)网格视图
UISwitchSwitch开关控件
UISliderSeekBar滑动条
UIProgressViewProgressBar进度条
UIAlertControllerAlertDialog弹窗对话框
UINavigationControllerFragment + Navigation导航控制
组件初始化方式对比

闭包概念深度解析

什么是闭包?

闭包(Closure) 是一个可以捕获和存储其所在上下文中任意常量和变量引用的自包含的函数块。简单来说,闭包就是一个**“能记住周围环境的函数”**。

Swift 中的闭包语法

1. 基本语法结构:

{ (参数列表) -> 返回类型 in// 闭包体return 结果
}

2. in 关键字的作用:

in 关键字是Swift闭包语法中的分隔符,用于分隔闭包的参数声明部分和执行体部分:

{ (参数列表) -> 返回类型 in// 闭包体 - 实际执行的代码
}
  • in 之前:闭包的"签名"部分(参数列表和返回类型)
  • in 之后:闭包的"实现"部分(具体的执行代码)

语法规则:

  • 当闭包有参数或指定返回类型时,in 是必须的
  • 当闭包无参数且无返回类型声明时,可以省略 in
  • in 总是在参数声明之后,执行体之前

3. 闭包的几种形式:

// 完整形式
let fullClosure: (Int, Int) -> Int = { (a: Int, b: Int) -> Int inreturn a + b
}// 简化形式(类型推断)
let simpleClosure = { (a: Int, b: Int) inreturn a + b
}// 更简化(省略return)
let shorterClosure = { (a: Int, b: Int) in a + b }// 最简化(使用$0, $1参数简写)
let shortestClosure = { $0 + $1 }

3. 立即执行闭包(IIFE - Immediately Invoked Function Expression):

// 这就是UI组件初始化中使用的模式
private let result: Int = {let a = 10let b = 20return a + b
}()  // 注意这里的 () 表示立即执行print(result)  // 输出: 30
Kotlin 中的对应概念

Kotlin 有闭包概念吗? 答案是:有的,但叫法不同

Kotlin 中对应的概念包括:

  1. Lambda 表达式
  2. 高阶函数
  3. 函数类型
  4. 匿名函数
Swift 闭包 vs Kotlin Lambda 对比
特性Swift 闭包Kotlin Lambda
基本语法{ param in body }{ param -> body }
参数简写$0, $1, $2it(单参数时)
类型声明(Int) -> String(Int) -> String
捕获变量自动捕获自动捕获
立即执行{ }()run { }
实际代码对比

Swift - 闭包的各种用法:

// 1. UI组件初始化(立即执行闭包)
private let titleLabel: UILabel = {let label = UILabel()label.text = "Hello"label.font = UIFont.boldSystemFont(ofSize: 16)return label
}()// 2. 数组操作
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }  // [2, 4, 6, 8, 10]
let filtered = numbers.filter { $0 > 3 }  // [4, 5]// 3. 异步回调
URLSession.shared.dataTask(with: url) { data, response, error in// 处理网络响应
}// 4. 事件处理
button.addAction(UIAction { _ inprint("按钮被点击")
}, for: .touchUpInside)

Kotlin - Lambda 的对应用法:

// 1. 延迟初始化(类似立即执行闭包)
private val titleLabel: TextView by lazy {TextView(this).apply {text = "Hello"setTypeface(null, Typeface.BOLD)textSize = 16f}
}// 或使用 run 函数(更接近Swift的立即执行闭包)
private val titleLabel: TextView = run {val label = TextView(this)label.text = "Hello"label.setTypeface(null, Typeface.BOLD)label.textSize = 16flabel
}// 2. 集合操作
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }  // [2, 4, 6, 8, 10]
val filtered = numbers.filter { it > 3 }  // [4, 5]// 3. 异步回调
retrofit.getData().enqueue(object : Callback<Data> {override fun onResponse(call: Call<Data>, response: Response<Data>) {// 处理响应}override fun onFailure(call: Call<Data>, t: Throwable) {// 处理错误}
})// 或使用协程
viewModelScope.launch {val data = repository.getData()// 处理数据
}// 4. 事件处理
button.setOnClickListener {println("按钮被点击")
}
闭包的核心特性

1. 捕获变量(Variable Capture):

// Swift
func makeIncrementer(incrementAmount: Int) -> () -> Int {var total = 0let incrementer: () -> Int = {total += incrementAmount  // 捕获了 total 和 incrementAmountreturn total}return incrementer
}let incrementByTwo = makeIncrementer(incrementAmount: 2)
print(incrementByTwo())  // 2
print(incrementByTwo())  // 4
// Kotlin
fun makeIncrementer(incrementAmount: Int): () -> Int {var total = 0return {total += incrementAmount  // 捕获了 total 和 incrementAmounttotal}
}val incrementByTwo = makeIncrementer(2)
println(incrementByTwo())  // 2
println(incrementByTwo())  // 4

2. 逃逸闭包 vs 非逃逸闭包:

// Swift - 非逃逸闭包(默认)
func performOperation(_ operation: () -> Void) {operation()  // 在函数返回前执行
}// Swift - 逃逸闭包
func performAsyncOperation(_ completion: @escaping () -> Void) {DispatchQueue.main.asyncAfter(deadline: .now() + 1) {completion()  // 在函数返回后执行}
}
// Kotlin - 所有lambda都可以"逃逸"
fun performOperation(operation: () -> Unit) {operation()  // 立即执行
}fun performAsyncOperation(completion: () -> Unit) {Handler(Looper.getMainLooper()).postDelayed({completion()  // 延迟执行}, 1000)
}
UI组件初始化中的闭包优势

iOS - 闭包初始化的优势:

// ✅ 优点:配置集中、类型安全、立即执行
private let customButton: UIButton = {let button = UIButton(type: .system)button.setTitle("点击我", for: .normal)button.backgroundColor = .systemBluebutton.layer.cornerRadius = 8button.translatesAutoresizingMaskIntoConstraints = falsereturn button
}()// 🔄 等价的Android写法
private val customButton: Button by lazy {Button(this).apply {text = "点击我"setBackgroundColor(ContextCompat.getColor(context, R.color.blue))background.cornerRadius = 8.dpToPx()}
}

为什么iOS偏爱闭包初始化?

  1. 配置集中:所有属性设置在一个地方
  2. 类型安全:编译时检查类型
  3. 立即执行:对象创建时就完成配置
  4. 代码清晰:避免在viewDidLoad中大量UI配置代码
  5. 性能优化:只执行一次,之后直接使用
总结

闭包的本质

  • Swift的闭包和Kotlin的Lambda本质上都是**“函数式编程”**的体现
  • 它们都能捕获上下文作为参数传递延迟执行
  • 主要区别在于语法糖使用习惯

学习建议

  • 如果熟悉Kotlin的Lambda,理解Swift闭包会很容易
  • 重点掌握立即执行闭包的UI初始化模式
  • 理解逃逸闭包的概念,这在异步编程中很重要

**Android - XML声明的优势:**
```xml
<!-- ✅ 优点:可视化编辑、资源管理、主题适配 -->
<Buttonandroid:id="@+id/customButton"android:text="点击我"android:background="@drawable/rounded_button"style="@style/PrimaryButton" />
事件处理对比

iOS - Target-Action 模式详解:

核心概念解释:

  • Target:目标对象(通常是 self,即当前视图控制器)
  • Action:要执行的方法(通过 #selector 指定)
  • for:触发事件的类型(如点击、长按等)
// 方式一:addTarget(传统方式)
customButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
//                    ↑      ↑                              ↑
//                  目标对象  要执行的方法                    触发事件类型@objc private func buttonTapped() {print("按钮被点击")
}// 方式二:闭包(iOS 14+,现代方式)
customButton.addAction(UIAction { _ inprint("按钮被点击")
}, for: .touchUpInside)

详细参数解释:

1. #selector 是什么?

  • #selector 是Swift中的选择器语法,用于将方法名转换为Objective-C可识别的选择器
  • 相当于告诉系统:“当事件发生时,调用这个方法”
  • #selector 引用的方法必须标记为 @objc

2. for 参数 - 事件类型:

事件类型含义Android对应
.touchUpInside手指在按钮内部抬起(标准点击)OnClickListener
.touchDown手指按下OnTouchListener.ACTION_DOWN
.touchUpOutside手指在按钮外部抬起无直接对应
.valueChanged值改变(如滑块、开关)OnSeekBarChangeListener
.editingChanged文本编辑中TextWatcher.afterTextChanged
.editingDidEnd文本编辑结束OnFocusChangeListener

3. 完整的事件绑定流程:

// 步骤1:创建按钮
let button = UIButton(type: .system)
button.setTitle("点击我", for: .normal)// 步骤2:绑定事件(Target-Action模式)
button.addTarget(self,                    // 目标:当前控制器action: #selector(handleButtonTap),  // 动作:要执行的方法for: .touchUpInside)     // 事件:手指在按钮内抬起// 步骤3:实现响应方法
@objc private func handleButtonTap() {print("按钮被点击了!")// 处理点击逻辑
}

Android - 监听器模式:

// 方式一:setOnClickListener
customButton.setOnClickListener {println("按钮被点击")
}// 方式二:XML中声明
// android:onClick="buttonTapped"
fun buttonTapped(view: View) {println("按钮被点击")
}
布局系统深度对比

iOS - Auto Layout 约束系统:

重要概念:view 是什么?

  • viewUIViewController 的根视图属性,相当于Android中的父容器
  • 它代表整个屏幕的主视图区域(除了状态栏、导航栏等系统UI)
  • 所有子视图都添加到这个 view 上,并相对于它进行布局约束

视图层级关系:

屏幕
├── 状态栏 (系统)
├── 导航栏 (可选)
└── view (ViewController的根视图) ← 约束代码中的 view├── helloLabel├── button└── 其他子视图...
// 代码方式设置约束
helloLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
//                                           ↑
//                                    ViewController的根视图
helloLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -50).isActive = true
helloLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
helloLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true// 或使用 NSLayoutConstraint
NSLayoutConstraint.activate([helloLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),helloLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100)//                                        ↑//                                   安全区域(避开刘海、底部指示器)
])

iOS vs Android 对比:

iOSAndroid含义
viewparent父容器/根布局
view.centerXAnchorparent(水平居中)父容器的水平中心
view.safeAreaLayoutGuide无直接对应安全区域布局指南

**Android - ConstraintLayout 约束系统:**
```xml
<!-- XML方式设置约束 -->
<androidx.constraintlayout.widget.ConstraintLayout><TextViewandroid:id="@+id/helloLabel"android:layout_width="200dp"android:layout_height="50dp"app:layout_constraintTop_toTopOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
// 代码方式设置约束
val constraintSet = ConstraintSet()
constraintSet.connect(helloLabel.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 100)
constraintSet.connect(helloLabel.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
constraintSet.connect(helloLabel.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
constraintSet.applyTo(constraintLayout)
样式和主题管理对比

iOS - 外观代理和样式:

// 全局样式设置
UILabel.appearance().font = UIFont.systemFont(ofSize: 16)
UIButton.appearance().tintColor = .systemBlue// 个别组件样式
helloLabel.layer.cornerRadius = 8
helloLabel.layer.borderWidth = 1
helloLabel.layer.borderColor = UIColor.systemGray.cgColor
helloLabel.backgroundColor = .systemBackground

Android - 样式和主题系统:

<!-- styles.xml -->
<style name="CustomLabelStyle"><item name="android:textSize">16sp</item><item name="android:textColor">@color/primary_text</item><item name="android:background">@drawable/rounded_background</item>
</style><!-- 应用样式 -->
<TextViewstyle="@style/CustomLabelStyle"android:text="Hello, World!" />
数据绑定和状态管理对比

iOS - 属性观察器和KVO:

class ViewController: UIViewController {@IBOutlet weak var countLabel: UILabel!var count: Int = 0 {didSet {countLabel.text = "计数: \(count)"}}// 或使用 Combine (iOS 13+)@Published var count: Int = 0override func viewDidLoad() {super.viewDidLoad()$count.map { "计数: \($0)" }.assign(to: \.text, on: countLabel).store(in: &cancellables)}
}

Android - 数据绑定和LiveData:

// 使用 LiveData 观察数据变化
class MainActivity : AppCompatActivity() {private val viewModel: MainViewModel by viewModels()override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// 观察数据变化viewModel.count.observe(this) { count ->findViewById<TextView>(R.id.countLabel).text = "计数: $count"}}
}
导航和页面跳转对比

iOS - Navigation Controller:

// 推入新页面
let detailVC = DetailViewController()
navigationController?.pushViewController(detailVC, animated: true)// 模态展示
let modalVC = ModalViewController()
present(modalVC, animated: true)// 使用 Storyboard Segue
performSegue(withIdentifier: "showDetail", sender: self)

Android - Intent 和 Fragment:

// Activity 跳转
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("data", "传递的数据")
startActivity(intent)// Fragment 切换
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, DetailFragment()).addToBackStack(null).commit()// 使用 Navigation Component
findNavController().navigate(R.id.action_main_to_detail)
核心差异总结
方面iOS 特点Android 特点
UI创建代码优先,闭包初始化XML优先,声明式布局
布局系统Auto Layout,约束驱动多种布局,ConstraintLayout推荐
样式管理代码设置,Appearance代理XML样式,主题系统完善
数据绑定手动绑定,Combine框架LiveData观察者模式
导航模式Navigation Controller栈Intent系统,Fragment管理
开发工具Interface Builder可选Layout Editor主流
预览功能SwiftUI PreviewLayout Preview
热重载有限支持Instant Run/Apply Changes
学习建议

从Android转iOS:

  1. 🎯 重点掌握Auto Layout约束系统
  2. 🎯 熟悉闭包初始化UI组件的模式
  3. 🎯 理解Target-Action事件处理机制
  4. 🎯 学习Navigation Controller的栈式导航

从iOS转Android:

  1. 🎯 掌握XML布局和ConstraintLayout
  2. 🎯 熟悉样式和主题系统
  3. 🎯 理解Activity和Fragment生命周期
  4. 🎯 学习Intent系统和数据传递

3. 生命周期方法

iOS:

override func viewDidLoad() {super.viewDidLoad()  // 相当于Android的onCreate()setupUI()           // 初始化UIsetupConstraints()  // 设置约束setupActions()      // 绑定事件
}

Android:

override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)  // 设置布局setupUI()          // 初始化UIsetupListeners()   // 绑定事件
}

4. 布局系统:Auto Layout vs ConstraintLayout

iOS Auto Layout (代码方式):

NSLayoutConstraint.activate([// 水平居中helloLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),// 垂直位置helloLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -60),// 边距约束helloLabel.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 20)
])

Android ConstraintLayout (XML方式):

<TextViewapp:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent" />

5. 事件处理:Target-Action vs Listener

iOS Target-Action 模式:

// 绑定事件
tapButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)// 事件处理方法
@objc private func buttonTapped() {tapCount += 1// 处理点击事件
}

Android Listener 模式:

// 绑定事件
tapButton.setOnClickListener {tapCount++// 处理点击事件
}

6. 动画系统

iOS UIView 动画:

UIView.animate(withDuration: 0.3, animations: {self.tapButton.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
}) { _ inUIView.animate(withDuration: 0.3) {self.tapButton.transform = .identity}
}

Android 属性动画:

val scaleDown = ObjectAnimator.ofFloat(tapButton, "scaleX", 1f, 0.95f)
val scaleUp = ObjectAnimator.ofFloat(tapButton, "scaleX", 0.95f, 1f)
AnimatorSet().apply {play(scaleDown).before(scaleUp)duration = 300start()
}

AppDelegate 与 SceneDelegate

历史背景

AppDelegate 是 iOS 应用的传统架构核心,从 iOS 2.0 开始就存在。而 SceneDelegate 是 iOS 13.0 引入的新概念,用于支持多窗口应用。

核心区别

职责范围

AppDelegate(应用级别):

  • 管理整个应用的生命周期
  • 处理应用启动、终止、后台等全局事件
  • 管理应用级别的配置和服务
  • 在 iOS 13 之前,也负责窗口管理

SceneDelegate(场景级别):

  • 管理单个窗口/场景的生命周期
  • 处理窗口的创建、激活、失活等事件
  • 支持多窗口应用(iPad 分屏、Mac Catalyst)
  • 只在 iOS 13+ 中可用
代码对比分析

AppDelegate.swift 中的关键代码:

// iOS 12 及以下的窗口管理方式
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {window = UIWindow(frame: UIScreen.main.bounds)window?.rootViewController = ViewController()window?.makeKeyAndVisible()return true
}// iOS 13+ 的场景配置方法
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

SceneDelegate.swift 中的关键代码:

// iOS 13+ 的窗口管理方式
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {guard let windowScene = (scene as? UIWindowScene) else { return }window = UIWindow(windowScene: windowScene)window?.rootViewController = ViewController()window?.makeKeyAndVisible()
}

执行流程

iOS 13+ 设备上的执行顺序:
  1. AppDelegate.application(_:didFinishLaunchingWithOptions:) - 应用启动
  2. AppDelegate.application(_:configurationForConnecting:options:) - 配置场景
  3. SceneDelegate.scene(_:willConnectTo:options:) - 创建窗口
  4. SceneDelegate.sceneDidBecomeActive(_😃 - 场景激活
iOS 12 及以下设备:
  1. AppDelegate.application(_:didFinishLaunchingWithOptions:) - 应用启动并创建窗口
  2. 直接使用 AppDelegate 管理窗口生命周期

AppDelegate 和 SceneDelegate 的连接机制

重要发现:AppDelegate 中没有直接引用 SceneDelegate

很多开发者会疑惑:AppDelegate 和 SceneDelegate 之间是如何连接的? 实际上,它们之间是"松耦合"的关系,通过系统配置文件连接,而不是直接的代码引用。

连接机制详解

1. Info.plist 配置文件是关键

<key>UISceneDelegate</key>
<dict><key>UIWindowSceneSessionRoleApplication</key><array><dict><key>UISceneConfigurationName</key><string>Default Configuration</string><key>UISceneDelegateClassName</key><string>$(PRODUCT_MODULE_NAME).SceneDelegate</string></dict></array>
</dict>

2. AppDelegate 的间接连接

// AppDelegate.swift 中的关键方法
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {// 返回配置名称,对应 Info.plist 中的 UISceneConfigurationNamereturn UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

3. 完整的连接流程

应用启动↓
AppDelegate.didFinishLaunchingWithOptions↓
系统请求场景配置↓
AppDelegate.configurationForConnecting 返回 "Default Configuration"↓
系统查找 Info.plist 中名为 "Default Configuration" 的配置↓
找到对应的 UISceneDelegateClassName: SceneDelegate↓
系统自动实例化 SceneDelegate 类↓
SceneDelegate.scene(_:willConnectTo:) 被调用↓
创建和管理窗口
关键理解点

松耦合设计的优势:

  • 灵活性:可以通过修改 Info.plist 更换不同的 SceneDelegate 类
  • 可配置性:支持多种场景配置,适应不同的应用需求
  • 向后兼容:iOS 12 及以下版本会忽略 Scene 相关配置
  • 系统管理:由系统负责实例化和生命周期管理,减少样板代码

为什么不直接引用?

  • 支持多场景应用(一个应用可以有多个不同的 SceneDelegate)
  • 允许动态配置场景类型
  • 保持架构的清晰分离

是否有必要都存在?

建议保留两者的情况:
  1. 需要支持 iOS 12 及以下版本

    • AppDelegate 处理旧版本的窗口管理
    • SceneDelegate 处理新版本的场景管理
  2. 计划支持多窗口功能

    • iPad 分屏应用
    • Mac Catalyst 应用
  3. 渐进式迁移

    • 保持向后兼容性
    • 逐步采用新架构
可以简化的情况:
  1. 只支持 iOS 13+

    • 可以移除 AppDelegate 中的窗口管理代码
    • 专注使用 SceneDelegate
  2. 简单的单窗口应用

    • 可以在 Info.plist 中移除 Scene 配置
    • 只使用 AppDelegate(传统方式)

开发工具与快捷键

Xcode 常用快捷键

导航和搜索
功能XcodeAndroid Studio (macOS)
全局搜索⌘ + Shift + F⌘ + Shift + F
文件搜索⌘ + Shift + O⌘ + Shift + O
类/符号搜索⌘ + Shift + O⌘ + O
快速打开⌘ + T⌘ + Shift + A
代码编辑
功能XcodeAndroid Studio (macOS)
代码补全Ctrl + SpaceCtrl + Space
格式化代码Ctrl + I⌘ + Alt + L
注释/取消注释⌘ + /⌘ + /
重命名⌘ + Ctrl + EShift + F6
跳转和导航
功能XcodeAndroid Studio (macOS)
跳转到定义⌘ + Ctrl + J⌘ + 点击⌘ + B⌘ + 点击
返回上一位置⌘ + Ctrl + ←⌘ + Alt + ←
前进下一位置⌘ + Ctrl + →⌘ + Alt + →
查找用法⌘ + Shift + FAlt + F7
构建和运行
功能XcodeAndroid Studio (macOS)
构建项目⌘ + B⌘ + F9
运行应用⌘ + RCtrl + R
停止运行⌘ + .⌘ + F2
清理构建⌘ + Shift + K无直接对应

自定义快捷键

在 Xcode 中自定义快捷键:

  1. 打开 Xcode → Preferences → Key Bindings
  2. 搜索要修改的功能
  3. 双击快捷键列进行修改
  4. 可以导出/导入快捷键配置文件

总结与建议

关键语法特点总结

  1. Swift vs Kotlin/Java

    • Swift 更函数式,大量使用闭包
    • 可选类型 (?) vs Nullable types
    • 属性观察器 vs 传统getter/setter
  2. 布局哲学

    • iOS:代码布局为主,约束系统
    • Android:XML布局为主,声明式
  3. 生命周期

    • iOS:viewDidLoadviewWillAppearviewDidAppear
    • Android:onCreateonStartonResume
  4. 事件处理

    • iOS:Target-Action 模式,需要 @objc 标记
    • Android:Listener 接口,Lambda 表达式

学习建议

  1. 从基础开始:理解 MVC 架构和生命周期
  2. 实践为主:多写代码,熟悉 Swift 语法
  3. 对比学习:利用已有的 Android 经验类比学习
  4. 关注差异:重点理解两个平台的设计哲学差异
  5. 工具熟练:掌握 Xcode 的使用和调试技巧

下一步学习方向

  1. UI 进阶:学习 UIKit 更多组件和布局技巧
  2. 数据管理:Core Data、UserDefaults、网络请求
  3. 架构模式:MVVM、Coordinator 等现代架构
  4. SwiftUI:苹果的声明式 UI 框架
  5. 性能优化:内存管理、性能调试工具

UIKit 框架详解

什么是 UIKit?

UIKit 是苹果公司为 iOS 和 tvOS 开发提供的核心用户界面框架。它是构建 iOS 应用程序用户界面的基础框架,提供了创建和管理应用程序界面所需的所有基本组件和功能。

UIKit 的核心定位

import UIKit  // 导入UIKit框架// UIKit是iOS应用开发的核心框架
// 几乎所有的UI相关类都来自UIKit
class ViewController: UIViewController {  // 继承自UIKit的UIViewControllerlet label = UILabel()      // UIKit的标签组件let button = UIButton()    // UIKit的按钮组件let imageView = UIImageView()  // UIKit的图片组件
}

UIKit 框架的组成部分

1. 视图和控件 (Views and Controls)

基础视图类:

  • UIView - 所有视图的基类
  • UILabel - 文本显示
  • UIButton - 按钮控件
  • UIImageView - 图片显示
  • UITextField - 文本输入框
  • UITextView - 多行文本编辑

容器视图:

  • UIScrollView - 滚动视图
  • UITableView - 表格视图
  • UICollectionView - 集合视图
  • UIStackView - 堆栈视图

控制组件:

  • UISwitch - 开关
  • UISlider - 滑动条
  • UISegmentedControl - 分段控制
  • UIProgressView - 进度条
  • UIStepper - 步进器
2. 视图控制器 (View Controllers)
// 视图控制器是UIKit的核心概念
class MyViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()// 视图加载完成后的初始化}override func viewWillAppear(_ animated: Bool) {super.viewWillAppear(animated)// 视图即将出现时的处理}
}// 特殊的视图控制器
class MyTableViewController: UITableViewController {// 专门用于管理表格视图
}class MyNavigationController: UINavigationController {// 用于导航管理
}
3. 布局系统 (Layout System)

Auto Layout:

// UIKit的约束布局系统
NSLayoutConstraint.activate([label.centerXAnchor.constraint(equalTo: view.centerXAnchor),label.centerYAnchor.constraint(equalTo: view.centerYAnchor),label.widthAnchor.constraint(equalToConstant: 200),label.heightAnchor.constraint(equalToConstant: 50)
])

Frame-based Layout:

// 传统的坐标布局方式
label.frame = CGRect(x: 100, y: 200, width: 200, height: 50)
4. 事件处理系统 (Event Handling)

Target-Action 模式:

// UIKit的事件处理机制
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)@objc func buttonTapped() {print("按钮被点击了")
}

手势识别:

// 手势识别器
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tapGesture)
5. 动画系统 (Animation System)
// UIKit提供的动画API
UIView.animate(withDuration: 0.3) {self.view.backgroundColor = .redself.label.alpha = 0.5
}// 更复杂的动画
UIView.animate(withDuration: 0.5, delay: 0.1,usingSpringWithDamping: 0.8,initialSpringVelocity: 0.2,options: .curveEaseInOut) {self.button.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}

UIKit 与其他框架的关系

UIKit vs SwiftUI
特性UIKitSwiftUI
发布时间2008年 (iOS 2.0)2019年 (iOS 13.0)
编程范式命令式编程声明式编程
布局方式Auto Layout + Frame声明式布局
状态管理手动管理自动响应式
学习曲线较陡峭相对平缓
兼容性iOS 2.0+iOS 13.0+
// UIKit 方式
class UIKitViewController: UIViewController {@IBOutlet weak var label: UILabel!override func viewDidLoad() {super.viewDidLoad()label.text = "Hello UIKit"}
}// SwiftUI 方式
struct SwiftUIView: View {var body: some View {Text("Hello SwiftUI")}
}
UIKit 与 Foundation 的关系
import Foundation  // 基础框架
import UIKit      // UI框架,依赖Foundation// Foundation提供基础数据类型
let string: String = "Hello"        // Foundation
let array: Array<Int> = [1, 2, 3]   // Foundation
let url: URL = URL(string: "https://apple.com")!  // Foundation// UIKit提供UI组件
let label: UILabel = UILabel()       // UIKit
let button: UIButton = UIButton()    // UIKit
let view: UIView = UIView()          // UIKit

UIKit 的核心设计模式

1. MVC (Model-View-Controller)
// Model - 数据模型
struct User {let name: Stringlet age: Int
}// View - 视图 (UIKit组件)
class UserView: UIView {let nameLabel = UILabel()let ageLabel = UILabel()
}// Controller - 控制器
class UserViewController: UIViewController {let userView = UserView()var user: User?override func viewDidLoad() {super.viewDidLoad()updateUI()}func updateUI() {userView.nameLabel.text = user?.nameuserView.ageLabel.text = "\(user?.age ?? 0)"}
}
2. Delegate 模式
// UIKit大量使用委托模式
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return 10}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)return cell}
}

UIKit 的优势和特点

优势:
  1. 成熟稳定 - 经过15年的发展和优化
  2. 功能完整 - 提供了构建复杂应用所需的所有组件
  3. 性能优秀 - 经过高度优化,运行效率高
  4. 兼容性好 - 支持从iOS 2.0到最新版本
  5. 生态丰富 - 大量第三方库和资源
  6. 精确控制 - 可以精确控制每个像素和动画
特点:
  1. 命令式编程 - 需要明确告诉系统如何做
  2. 手动内存管理 - 需要注意循环引用等问题
  3. 生命周期管理 - 需要理解视图控制器的生命周期
  4. 事件驱动 - 基于事件和回调的编程模式

学习 UIKit 的建议

学习路径:
  1. 基础概念 - 理解视图、视图控制器、生命周期
  2. 基本组件 - 掌握常用的UI组件使用
  3. 布局系统 - 学习Auto Layout约束布局
  4. 事件处理 - 理解Target-Action和手势识别
  5. 高级特性 - 动画、自定义控件、性能优化
实践建议:
  1. 从简单开始 - 先做Hello World类型的应用
  2. 多写代码 - UIKit需要大量实践才能熟练
  3. 阅读文档 - 苹果官方文档是最好的学习资源
  4. 参考示例 - 学习优秀的开源项目代码
  5. 调试技巧 - 掌握Xcode的调试和性能分析工具

总结

UIKit 是 iOS 开发的核心框架,它提供了构建用户界面所需的所有基础组件和功能。虽然苹果推出了 SwiftUI 作为新的 UI 框架,但 UIKit 仍然是 iOS 开发的重要基础,特别是在需要兼容旧版本 iOS 或需要精确控制 UI 的场景下。

对于初学者来说,理解 UIKit 的核心概念(视图、视图控制器、生命周期、事件处理)是掌握 iOS 开发的关键。通过实际项目练习,逐步掌握 UIKit 的各种组件和特性,是成为优秀 iOS 开发者的必经之路。


文章转载自:

http://CSYAOLHf.Ltpzr.cn
http://flCaJlwg.Ltpzr.cn
http://3CMo3Q6I.Ltpzr.cn
http://IGKzKTxb.Ltpzr.cn
http://GaFYGI6O.Ltpzr.cn
http://teiBcghk.Ltpzr.cn
http://zE0r8Vd9.Ltpzr.cn
http://egBn3VCj.Ltpzr.cn
http://3cH69qOX.Ltpzr.cn
http://VdhAKX2d.Ltpzr.cn
http://OqXnseqx.Ltpzr.cn
http://Av9fXrTg.Ltpzr.cn
http://hnRq31p1.Ltpzr.cn
http://DMdaJf1h.Ltpzr.cn
http://btw0OgrI.Ltpzr.cn
http://oYppRvtC.Ltpzr.cn
http://zJFBuJRg.Ltpzr.cn
http://FAaLRPQq.Ltpzr.cn
http://01yzrtSe.Ltpzr.cn
http://Mt8yKGik.Ltpzr.cn
http://AzK6Zs3V.Ltpzr.cn
http://gfRdqgUu.Ltpzr.cn
http://yT5lhRId.Ltpzr.cn
http://Judjf0Ke.Ltpzr.cn
http://huVvZnuO.Ltpzr.cn
http://sYJ7zyy7.Ltpzr.cn
http://8sqSDuJf.Ltpzr.cn
http://ssdXaqit.Ltpzr.cn
http://GXEraZ17.Ltpzr.cn
http://WsX6dUyR.Ltpzr.cn
http://www.dtcms.com/a/373324.html

相关文章:

  • HCIE数通/云计算真机实验机架展示
  • 【.Net技术栈梳理】04-核心框架与运行时(线程处理)
  • 量化金融|基于算法和模型的预测研究综述
  • HarmonyOS 数据处理性能优化:算法 + 异步 + 分布式实战
  • 1304. 和为零的 N 个不同整数
  • Java 集合Collection—List
  • leetcode9(跳跃游戏)
  • 在UnionTech OS Server 20 (统信UOS服务器版) 上离线安装PostgreSQL (pgsql) 数据库
  • Azure Logic App 与 Azure Function 对比分析
  • 房屋安全鉴定注意事项
  • 【Go】:mac 环境下GoFrame安装开发工具 gf-cli——gf_darwin_arm64
  • 知识竞赛活动舞台道具全面指南
  • Linux《进程信号(下)》
  • 力扣.1054距离相等的条形码力扣767.重构字符串力扣47.全排列II力扣980.不同路径III力扣509.斐波那契数列(记忆化搜索)
  • 区块链:重构企业数字化的信任核心与创新动力
  • 【系统架构设计师(22)】面向服务的软件架构风格
  • Google Play账户与App突遭封禁?紧急应对与快速重构上架策略
  • 操作系统进程/线程的状态与转换
  • 保姆级教程 | travis-Linux版本安装编译
  • 【HarmonyOS 6】Install Failed: error: failed to install bundle.code:9568322
  • STM32精准控制水流
  • Failed to connect to github.com port 443 after 21s
  • 视频画质差怎么办?AI优化视频清晰度技术原理与实战应用
  • comfyUI 暴露网络restful http接口
  • Python程序使用了Ffmpeg,结束程序后,文件夹中仍然生成音频、视频文件
  • 【CFA三级笔记】资产配置:第二章 资本市场预期(预测资产收益)
  • CSS3核心技术
  • Redis 发布订阅模式:轻量级消息系统实战指南
  • 简单粗暴的Linux入门以及基础命令
  • SME-Econometrics