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/