作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
let scene = UIApplication.shared.connectedScenes.first
if let sd: SceneDelegate = (scene?.delegate as? SceneDelegate) {
let window = sd.window
}
这是用 Swift 编写的,我想将其转换为 Objective-C。我已经尝试过这个:
UIScene *scene = [[[[UIApplication sharedApplication]connectedScenes]allObjects]firstObject];
但是现在没有SceneDelegate
;在 Objective-C 中有 UIKit 的 UIWindowSceneDelegate
.
我也无法访问 UIScenedelegate。 Scenedelegate 在 Swift 中,我试图在 Objective-C 中获取窗口但现在我无法访问 swiftUI 中的窗口。
最佳答案
检查scene.delegate
以确保它符合协议(protocol),然后将协议(protocol)转换到它上面,以便编译器允许您使用协议(protocol)属性和/或方法。
UIScene *scene = [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];
if([scene.delegate conformsToProtocol:@protocol(UIWindowSceneDelegate)]){
UIWindow *window = [(id <UIWindowSceneDelegate>)scene.delegate window];
}
关于objective-c - SwiftUI:如何在 Objective-C 中获取窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59613517/
我是一名优秀的程序员,十分优秀!