Xcode14开发iOS应用运行后先显标签再黑屏求助
iOS应用运行后显示标签一秒随即黑屏问题
我正在用Xcode 14开发一款新iOS应用,已经创建了主故事板,并且在ViewController上添加了一个标签。但运行应用时,标签只会先显示一秒钟,之后屏幕就变黑了,而且没有任何报错信息。
我尝试过在AppDelegate中添加var window: UIWindow?,但应用还是会出现黑屏问题。
以下是我的代码,恳请帮忙解决:
AppDelegate代码
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代码
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() } }
内容的提问来源于stack exchange,提问作者StalicJi
相关产品推荐
相关产品推荐

