在 iOS 应用中关闭所有页面的常见方法包括:
1. 使用 `UIViewController` 的 `dismiss(animated:completion:)` 方法:
- 如果当前页面是模态呈现的, 可以调用 `self.dismiss(animated: true, completion: nil)` 来关闭当前页面.
- 如果需要关闭整个导航栈, 可以从当前控制器开始, 一直向上 dismiss 直到根控制器.
2. 使用 `UINavigationController` 的 `popToRootViewController(animated:)` 方法:
- 如果当前页面是通过 `UINavigationController` 管理的, 可以调用 `self.navigationController?.popToRootViewController(animated: true)` 来关闭所有页面, 返回到根页面.
3. 使用 `UITabBarController` 的 `selectedIndex` 属性:
- 如果当前页面是通过 `UITabBarController` 管理的, 可以设置 `self.tabBarController?.selectedIndex = 0` 来切换到根页面.
4. 使用 `UIApplication` 的 `keyWindow?.rootViewController?.dismiss(animated:completion:)` 方法:
- 这个方法可以关闭整个应用的所有页面, 并返回到应用的根视图控制器.
需要注意的是, 在关闭所有页面时, 要确保应用的导航和状态管理保持一致,避免出现意外的行为.