使用场景委托,我可以设置根视图控制器。(我使用的是 Xcode 11.3 和 iOS 版本 13.3,并在 iPhone 6+ 和 iOS 12.4 上运行我的应用程序)
我想要的是当用户登录时,我需要更新根视图控制器。为此,我做了以下
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
static let shared = SceneDelegate()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//some code is here
}
}
@available(iOS 13.0, *)
extension SceneDelegate {
func setRootViewControllerBasedOnLogin() {
if let isLoggedIn = UserDefaults.standard.bool(forKey: "isLogin"), isLoggedIn {
let tabbar = UIStoryboard(name: "Other", bundle: nil).instantiateViewController(withIdentifier: "Tabbar") as! UITabBarController
if var vcs = tabbar.viewControllers {
vcs.remove(at: 2)
tabbar.viewControllers = vcs
}
self.window?.rootViewController = tabbar
} else {
//other stuff
}
}
}
因此,当用户登录应用程序时,我需要从标签栏中删除一个标签项并更新根视图控制器。
所以我这样做如下。
func processLogin() {
//performing login in this method so when login successful we setting root view controller
callLoginAPI { response in
if response.isSuccess {
UserDefaults.standard.set(true, forKey: "isLogin")
if #available(iOS 13.0, *) {
SceneDelegate.shared.setRootViewControllerBasedOnLogin()
} else {
// Fallback on earlier versions
}
}
}
}
当我这样做时,什么也没发生。用户成功登录应用后,我无法更改应用的根视图控制器?
有什么建议么?我究竟做错了什么?