升级Xcode后,我的应用程序的关键部分已停止工作。
启动我的应用程序时,我运行一个函数来检查布尔标志并设置正确的rootViewController。
但是我一直在设置它的代码现在已经停止工作
class func setLoginAsInitialViewContoller(window:UIWindow) {
print("SET LOGIN")
let storyboard = UIStoryboard(name: "Login", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
controller.modalPresentationStyle = .overFullScreen
window.rootViewController = controller
window.makeKeyAndVisible()
}
具体来说,当应用获得倒数第二行window.rootViewController = controller
时,它会崩溃并出现libc++abi.dylib: terminating with uncaught exception of type NSException
错误。
上面的函数在名为Utilities.swift
的类中,我正在AppDelegate.swift
内部调用函数,如下所示:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var storyboard: UIStoryboard? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isIdleTimerDisabled = true
Utilities.decideInitialViewController(window: self.window!)
return true
}
非常感谢我设置根控制器的任何解决方案或修补程序。
谢谢!
答案 0 :(得分:4)
这是因为AppDelegate.swift不再具有window
属性。
现在,您必须使用SceneDelegate.swift更改根视图控制器。
如本例所示:
Accessing the Root View Controller at Launch on iOS 13 with SceneDelegate
答案 1 :(得分:4)
它在您项目中的 SceneDelegate.swift 文件中可用
它将具有委托方法:
func scene(_ scene:UIScene,willConnectTo会话:UISceneSession, 选项connectionOptions:UIScene.ConnectionOptions)
示例
func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
let initialViewController =
storyboard.instantiateViewController(withIdentifier: "FirstViewController")
self.window!.rootViewController = initialViewController
self.window!.makeKeyAndVisible()
}
}
答案 2 :(得分:3)
在 viewDidAppear 中,您可以设置root:-
{
"compilerOptions": {
"lib": ["es2020"],
"module": "commonjs",
"target": "es2019"
}
}
答案 3 :(得分:0)
ScenceDelegate
。在scene(_:willConnectTo:options:
中,创建一个新的UIWindow,设置该窗口的rootViewController
,并使该窗口成为key
窗口。ScenceDelegate
,则可以尝试将其by following this删除。答案 4 :(得分:0)
对于希望创建几个扩展来更改根视图控制器并且需要支持两种委托类型(UISceneDelegate和AppDelegate)的任何人,这里有几个:
extension UIViewController {
var appDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
var sceneDelegate: SceneDelegate? {
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let delegate = windowScene.delegate as? SceneDelegate else { return nil }
return delegate
}
}
如果您需要扩展来从具有iOS12和iOS13支持的ViewController访问UIWindow:
extension UIViewController {
var window: UIWindow? {
if #available(iOS 13, *) {
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let delegate = windowScene.delegate as? SceneDelegate, let window = delegate.window else { return nil }
return window
}
guard let delegate = UIApplication.shared.delegate as? AppDelegate, let window = delegate.window else { return nil }
return window
}
}