【问题标题】:UINavigationController won't pushViewControllerUINavigationController 不会 pushViewController
【发布时间】:2019-12-23 15:37:48
【问题描述】:

我有两个 ViewController。一个叫做 LoginVC,它也是我的 rootviewcontroller,另一个叫做 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)
}

有人可以向我解释我做错了什么吗?

【问题讨论】:

  • LoginVC 是否显示?不确定这是否只是一个错字,但您还需要添加window.makeKeyAndVisible()。除此之外,它看起来还不错。
  • 检查LoginVC是否有navigationController
  • 您是否从 .plist 的主情节提要中删除情节提要?您的 appdelegate 上缺少 self.window?.makeKeyAndVisible() 让我觉得您正在打开默认故事板初始 VC,但您认为您是从 didFinishLaunchingWithOptions 打开它。
  • 感谢大家的回复!我添加了 window.makeKeyAndVisible() 并从我的 .plist 中删除了“Main”,但它仍然无法正常工作。
  • @MattiasTörnqvist 我的回答有效吗?

标签: swift uinavigationcontroller pushviewcontroller


【解决方案1】:

您应该以这种方式使用 File 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

【讨论】:

  • 我正在为 iOS 12 做这个,它是教程的一部分。
【解决方案2】:

直接初始化 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)

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

【讨论】:

  • 我没有使用 Storyboard,而是尝试以编程方式进行。
【解决方案3】:

这在 SceneDelegate 文件中起到了作用:

    @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()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2012-01-17
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多