我有两个ViewControllers。一个叫做LoginVC,也是我的根视图控制器,另一个叫做SignUpVC。
在我的AppDelegate中,我这样设置了我的UINavigationbar:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.rootViewController = UINavigationController(rootViewController: LoginVC())
return true
}然后在我的LoginVC中,我使用此命令来显示我的SignUpVC,但它不起作用。
@objc func handleShowSignUp() {
let signUpVC = SignUpVC()
navigationController?.pushViewController(signUpVC, animated: true)
}有人能给我解释一下我哪里做错了吗?
发布于 2019-12-24 00:25:04
您应该以这种方式使用文件SceneDelegate:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? // create Window
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let LoginVC = ViewController() //create your viewController
nav.pushViewController(contentView, animated: true)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = LoginVC
SceneDelegate.window = window
window.makeKeyAndVisible()
}
}重要提示:您可以检查UIWindowScene
发布于 2019-12-24 00:48:30
直接初始化LoginVC()和SignUpVC()是错误的。如果您使用的是故事板,请尝试以下内容
let storyboard = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let nav = storyboard.instantiateInitialViewController() //if you want the initial one
let storyBoard: UIStoryboard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let someVC: SomeViewController = giftStoryBoard.instantiateViewController(withIdentifier: "SomeViewController") as! SomeViewController //don't forget to set identifier on interfacebuilder如果您只使用.xib
let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)然后尝试将其推送到导航控制器
发布于 2019-12-24 05:23:33
这在SceneDelegate-file中起到了作用:
@available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: scene)
window?.rootViewController = UINavigationController(rootViewController: LoginVC())
window?.makeKeyAndVisible()
}https://stackoverflow.com/questions/59457861
复制相似问题