首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UINavigationController不会pushViewController

UINavigationController不会pushViewController
EN

Stack Overflow用户
提问于 2019-12-23 23:37:48
回答 3查看 147关注 0票数 0

我有两个ViewControllers。一个叫做LoginVC,也是我的根视图控制器,另一个叫做SignUpVC。

在我的AppDelegate中,我这样设置了我的UINavigationbar:

代码语言:javascript
复制
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow()
    window?.rootViewController = UINavigationController(rootViewController: LoginVC())

    return true
}

然后在我的LoginVC中,我使用此命令来显示我的SignUpVC,但它不起作用。

代码语言:javascript
复制
@objc func handleShowSignUp() {
    let signUpVC = SignUpVC()
    navigationController?.pushViewController(signUpVC, animated: true)
}

有人能给我解释一下我哪里做错了吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-12-24 00:25:04

您应该以这种方式使用文件SceneDelegate:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2019-12-24 00:48:30

直接初始化LoginVC()和SignUpVC()是错误的。如果您使用的是故事板,请尝试以下内容

代码语言:javascript
复制
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

代码语言:javascript
复制
let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)

然后尝试将其推送到导航控制器

票数 0
EN

Stack Overflow用户

发布于 2019-12-24 05:23:33

这在SceneDelegate-file中起到了作用:

代码语言:javascript
复制
    @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()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59457861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档