UINavigationController不会pushViewController

时间:2019-12-23 15:37:48

标签: swift uinavigationcontroller pushviewcontroller

我有两个ViewController。一个叫做LoginVC,它也是我的rootviewcontroller,另一个叫做SignUpVC。

在我的AppDelegate中,我将UINavigationbar设置如下:

TextView1.setOnClickListener{
    TextView1.setBackgroundResource(R.color.red)
    Handler().postDelayed({
         TextView1.setBackgroundResource(R.color.white)
    }, 1_000)
    //return immediately so that the message loop can render the red background
}

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

var window: UIWindow?

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

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

    return true
}

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

这可以解决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()
}