在iOS 13.0、Xcode 11.2.1和Swift 5中,rootViewController未更新
我正在处理一个应用程序,我想在一个特定点上重置我的根ViewController。因此,我在在iOS 13.0、Xcode 11.2.1和Swift 5中,rootViewController未更新,ios,swift,xcode,rootviewcontroller,uiscenedelegate,Ios,Swift,Xcode,Rootviewcontroller,Uiscenedelegate,我正在处理一个应用程序,我想在一个特定点上重置我的根ViewController。因此,我在AppDelegate类中创建了一个方法来更新RootViewController extension AppDelegate{ func setStoryboard(name:String, controllerIdentifier:String){ let storyBoard : UIStoryboard = UIStoryboard(name: name, bundle:n
AppDelegate
类中创建了一个方法来更新RootViewController
extension AppDelegate{
func setStoryboard(name:String, controllerIdentifier:String){
let storyBoard : UIStoryboard = UIStoryboard(name: name, bundle:nil)
let controller = storyBoard.instantiateViewController(withIdentifier: controllerIdentifier)
let navigationController = UINavigationController(rootViewController: controller)
navigationController.setNavigationBarHidden(true, animated: false)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController = navigationController
} }
这对我来说很好,但每当我在iOS 13.0模拟器上运行我的应用程序时,我的应用程序就会在appDelegate.window!上崩溃!。rootViewController=navigationController
,因为window
正在获取nil
。正如我们所知,在iOS 13.0中,AppDelegae
类没有window
属性,它移动到了SceneDelegate
类中
所以,我在SceneDelegate
类的扩展中创建了一个新方法,如下所示
@available(iOS 13.0, *)
extension SceneDelegate{
func setStoryboard(name:String, controllerIdentifier:String){
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyBoard : UIStoryboard = UIStoryboard(name: name, bundle:nil)
let controller = storyBoard.instantiateViewController(withIdentifier: controllerIdentifier)
let navigationController = UINavigationController(rootViewController: controller)
navigationController.setNavigationBarHidden(true, animated: false)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
}
}
现在我的应用程序不再崩溃了。这很好,但是我的rootViewController
也没有得到更新
请让我知道我在SceneDelegate
课程中做错了什么。我正在获取窗口,但未更新rootViewController
我调用我的方法如下
@IBAction func btn_Close_Tapped(_ sender: UIButton) {
if #available(iOS 13.0, *) {
SceneDelegate.shared.setStoryboard(name: STORYBOARDS_NAME.HOME.rawValue, controllerIdentifier: IDENTIFIRES.HOME_TABBAR.rawValue)
} else {
AppDelegate.shared.setStoryboard(name: STORYBOARDS_NAME.HOME.rawValue, controllerIdentifier: IDENTIFIRES.HOME_TABBAR.rawValue)
}
}
“其他”部分对我来说很好。尝试使用它可能会对您有所帮助。我相信您需要先使用
UIApplication.shared.windows代码>。假设您使用的是单个窗口应用程序。@SwetaVani我已经试过了one@MaticOblak是的,我使用的是单窗口应用程序。@MaticOblak,谢谢你,它帮助我摆脱了keyWindow折旧警告。