首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行应用程序中的黑色屏幕

运行应用程序中的黑色屏幕
EN

Stack Overflow用户
提问于 2022-10-17 17:33:14
回答 1查看 40关注 0票数 0

在这里输入图像描述

我正在尝试用Xcode开发一个新的iOS应用程序。我制作了一个主要的故事板,并在我的ViewController上添加了一个标签。当我运行我的应用程序时,首先它会显示标签,然后变成黑色屏幕,没有任何错误。

我在做Xcode 14

我尝试添加(var窗口: UIWindow?)在我的AppDelegate,但应用程序仍然崩溃。

这是我的密码,请帮我:

AppDelegate

代码语言:javascript
复制
import UIKit

import CoreData

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  
    return true
}


func applicationWillTerminate(_ application: UIApplication) {
    self.saveContext()
}



// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "DataModel")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
 
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

// MARK: - Core Data Saving support

func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do {
            try context.save()
        } catch {
           
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }
}

}

SceneDelegate

代码语言:javascript
复制
import UIKit


class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  
    
    guard let _ = (scene as? UIWindowScene) else { return }
    

}

func sceneDidDisconnect(_ scene: UIScene) {

}

func sceneDidBecomeActive(_ scene: UIScene) {

}

func sceneWillResignActive(_ scene: UIScene) {

}

func sceneWillEnterForeground(_ scene: UIScene) {

}

func sceneDidEnterBackground(_ scene: UIScene) {

    (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
}


}
EN

回答 1

Stack Overflow用户

发布于 2022-10-18 09:34:59

试着添加

代码语言:javascript
复制
var window: UIWindow?

你的应用程序代表

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74101075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档