我看过很多与此错误相关的其他 stackoverflow 帖子,但在我的情况下我无法理解它(因此不知道如何解决)。
stackoverflow 帖子中的大多数回复(例如this例如,其中一个)涉及多个视图控制器,以及如何在前一个视图控制器完成之前将一个视图控制器推入堆栈。
但就我而言,我只有一个视图控制器,其 UI 非常非常非常小 - 我在示例测试代码中遇到了此错误。
项目中只有一个视图控制器,
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
NSLog(TAG + "ViewController.viewDidLoad")
super.viewDidLoad()
view.backgroundColor = .systemBlue
}
}
视图控制器在中初始化场景(willConnectTo:选项)UI 如下图所示。但中间有一个自定义类(Presentation)。
这是我的场景(willConnectTo:选项)
// uses Presentation class to display UI
func scene(_ pUIScene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
NSLog(TAG + "SceneDelegate.scene(willConnectTo:options)")
AppDelegate.sForegroundScene = pUIScene
var user_interface = Presentation()
user_interface.CreateWindow()
}
这是我的演示课,
class Presentation {
var window: UIWindow?
init() {
NSLog(TAG + "Presentation.init")
}
func CreateWindow() {
NSLog(TAG + "Presentation.CreateWindow")
guard let winScene = (AppDelegate.sForegroundScene as? UIWindowScene) else {
NSLog(TAG + "Error in obtaining UIWindowScene!")
return
}
window = UIWindow(windowScene: winScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
}
现在,如果我删除Presentation类并直接初始化ViewController并在场景中设置UI(willConnectTo:option) - 如下所示,它会按预期工作 - 我会看到蓝屏。
// Presentation class is not used
func scene(_ pUIScene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
NSLog(TAG + "SceneDelegate.scene(willConnectTo:options)")
AppDelegate.sForegroundScene = pUIScene
guard let winScene = (AppDelegate.sForegroundScene as? UIWindowScene) else {
NSLog(TAG + "Error in obtaining UIWindowScene!")
return
}
window = UIWindow(windowScene: winScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
为什么将 UI 代码移动到不同的类会导致“开始/结束外观转换的不平衡调用”问题?