gpt4 book ai didi

frameworks - UIApplication.sharedApplication().keyWindow 在框架测试中为零

转载 作者:行者123 更新时间:2023-12-03 15:00:03 27 4
gpt4 key购买 nike

我正在尝试测试(使用 XCTest 框架)驻留在框架(动态库)内的 View Controller 。我遇到了属性 UIApplication.sharedApplication().keyWindow 的问题,该属性为零。我有以下代码:

override func setUp() {
super.setUp()

let storyboard = UIStoryboard(name: STORYBOARD_NAME_REPORT,
bundle: BundleUtil.getCurrentBundle())
let chatViewController = storyboard.instantiateViewControllerWithIdentifier(VIEW_CONTROLLER_ID_TIP_CHAT) as UIViewController

let navigationController = UINavigationController()
let viewControllers = [chatViewController]
navigationController.viewControllers = viewControllers

// PROBLEM: The following causes a crash because keyWindow is nil.
UIApplication.sharedApplication().keyWindow!.rootViewController = chatViewController

// Force the view controller to be loaded.
// Source: https://www.natashatherobot.com/ios-testing-view-controllers-swift/
let _ = navigationController.view
let _ = chatViewController.view
}

任何想法如何使 keywindow 属性正确初始化?

最佳答案

您可以尝试如下设置:

首先获取共享窗口并将 View Controller 分配为根 Controller

var window = UIApplication.shared.delegate?.window!
window.rootViewController = viewController

然后使窗口键可见。
window.makeKeyAndVisible()
CATransaction.flush() //optional

关于frameworks - UIApplication.sharedApplication().keyWindow 在框架测试中为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37685634/

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