ios 原因:'-[FaveoHelpdeskPro_Swift.AppDelegate窗口]:无法识别的选择器已发送到示例0x 282 efc 980 '

xriantvc  于 2023-06-25  发布在  iOS
关注(0)|答案(4)|浏览(118)

我得到以下错误时,我试图运行项目,我的项目崩溃。
它给出以下错误,
FaveoHelpdeskPro_Swift[1400:370341] -[FaveoHelpdeskPro_Swift.AppDelegate窗口]:已发送到示例0x 282 efc 980无法识别的选择器

***由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:'-[FaveoHelpdeskPro_Swift.AppDelegate窗口]:无法识别的选择器已发送到示例0x 282 efc 980 '

进入下一屏幕,

而我的SceneDelegate.swift已经包含窗口对象,

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let _ = (scene as? UIWindowScene) else { return }
}

问题是什么,为什么会崩溃?
Here is the complete project

o4hqfura

o4hqfura1#

AppDelegate不包含window引用。在AppDelegate中添加window变量

var window: UIWindow?

摘自Apple的documentation
此属性包含用于在设备主屏幕上显示应用程序可视内容的窗口。

如果应用的Info.plist文件包含UIainStoryboardFile键,则需要实现此属性

w7t8yxp5

w7t8yxp52#

当你开始初始化项目中SceneDelegate中的主ViewController时,一些UI库(如SVProgressHUD,...)仍然指向AppDelegate的window,没有任何内容,所以它崩溃了。我遇到并成功解决了这个问题,如下所示:
1.在AppDelegate中声明一个UIWindow,如果它不存在,也声明一个静态变量以快速访问AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow? // HERE, add it if it's not available.
    static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
    //...
}

1.在SceneDelegate中,将UIWindow对象引用回AppDelegate:

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow? // HERE

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        window?.rootViewController = YOUR_VIEW_CONTROLLER()
        window?.makeKeyAndVisible()
        AppDelegate.shared.window = window // Connect it HERE!
    }
    // ....
}

注意:如果您刚刚完成第1步,它不会崩溃,但某些UI组件将显示错误,因为它们无法确定哪个是主窗口。

quhf5bfb

quhf5bfb3#

所以我刚才做的是在AppDelegate中添加一个UIWindow var,如下所示:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

...

然后在SceneDelegate中,当我设置UIWindow属性时:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.rootViewController = MainTabBarController()
        window?.windowScene = windowScene
        window?.makeKeyAndVisible()

我也为AppDelegate设置了它,就像下面这样:

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
    appDelegate.window = window
}
chhqkbe1

chhqkbe14#

我得到这个错误是因为使用SVProgressHUD,我的解决方案和@ngbaanh一样,在AppDelegate中定义了一个窗口变量,并在SceneDelegate中引用了窗口

相关问题