我有一个initialViewController,如果找到用户登录信息,会将用户路由到Home,但是如果没有,我将注册/登录ViewController设置为root。我用断点测试了我的注册/登录ViewController didLoad和didAppear变得可以执行,但是我的注册/登录ViewController没有显示,并且InitialViewController仍然显示在屏幕上。这是我的代码:
public static func setRootViewController(withId id: String, storyBoardName: String) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
let newRootVC = mainStoryboard.instantiateViewController(withIdentifier: id)
appDelegateRef.window!.rootViewController = newRootVC
}
这是我的故事板的一部分:
这是什么问题?
答案 0 :(得分:0)
将此函数放在您的AppDelegate类中。您可以在视图控制器中的任何位置调用此函数。
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
public func present(viewController: UIViewController) {
guard let window = window else { return }
UIView.transition(with: window, duration: 0.5, options: .transitionFlipFromLeft, animations: {
window.rootViewController = viewController
}, completion: nil)
}
}
在您要用户注销的视图控制器中,以这种方式调用上面的函数:
public static func setRootViewController(withId id: String, storyBoardName: String) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
let newRootVC = mainStoryboard.instantiateViewController(withIdentifier: id)
guard let delegate = UIApplication.shared.delegate as? AppDelegate else { return }
delegate.present(viewController: newRootVC)
}
答案 1 :(得分:0)
在函数末尾使用appDelegateRef.window!.makeKeyAndVisible()
public static func setRootViewController(withId id: String, storyBoardName: String) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
let newRootVC = mainStoryboard.instantiateViewController(withIdentifier: id)
appDelegateRef.window!.rootViewController = newRootVC
appDelegateRef.window!.makeKeyAndVisible()
}