gpt4 book ai didi

objective-c - SwiftUI:如何在 Objective-C 中获取窗口

转载 作者:行者123 更新时间:2023-12-02 03:03:16 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com