ios - 在场景委托中作为背景之前获取对最后一次看到的 VC 的引用
问题描述
我有一个rootViewController
as UITableViewController
,当一个单元格被点击时,它会呈现一个模态 VC。在那些 VC 中,我正在制作动画,UIViewPropertyAnimator
问题是当应用程序在当前显示该 VC 的同时被发送到后台时,因为动画师没有正确停止并且我viewWillDissapear
没有在我停止动画师的地方被调用。
- 问题是如何在我的模型中获取对模态 VC 的引用,
sceneDidEnterBackground
以便我可以正确地停止动画师?
如果我想获得对我的 rootVC 的引用,这很容易,window.rootViewController
但是模态 VC 呢?
解决方案
我假设您的modal VC
IS 是TOPMOST屏幕,我认为它应该是您想要从中获得参考的最顶层屏幕。
如果是这样,那里有很多参考资料。但这是我上次需要它时使用的整个功能。
var windowScene: UIScene? = {
let windowScene = UIApplication.shared
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first
return windowScene
}()
var windowRootController: UIViewController? {
if let window = windowScene as? UIWindowScene {
return window.windows.last?.rootViewController
}
return UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
}
/// Category for any controller.
extension UIViewController {
/// Class function to get the current or top most screen.
class func current(controller: UIViewController? = windowRootController) -> UIViewController? {
guard let controller = controller else { return nil }
if let navigationController = controller as? UINavigationController {
return current(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return current(controller: selected)
}
}
if let presented = controller.presentedViewController {
return current(controller: presented)
}
return controller
}
}
所以基本上,就像这样称呼它let modalVC = UIViewController.current()
。
玩这段代码,就像我很确定你不需要windowScene
and一样windowRootViewController
,但不是 100% 确定。试试看。希望能帮助到你!
推荐阅读
- python - 如何根据条件创建多个新数据框,这些数据框是主数据框的子集并动态唯一地命名它们?
- python - 更改 DataFrame 的副本
- python - 从 sys 导入 exit 是运行内置函数还是模块中的函数?
- angular - 为什么以角度格式或矩格式格式化日期从日期开始随着时间 T00:00:00.000Z 减少
- reactjs - 将 dom 从子组件更新到父组件
- jquery - 如何根据 CSS 中的 ID 更改伪元素内容
- .net - 为什么 System.Text.Json.Serialization.JsonSerializer 编码双引号而不是仅仅转义它们?
- java - 无法添加子实体 - JPA OneToMany 关系
- c# - OPC-UA“值”和“属性值”有什么区别
- vue.js - 安装 VueJS CLI 时如何修复 npm WARN deprecated 错误