簡體   English   中英

推送沒有 storyboard 的 ViewController

[英]push ViewController without storyboard

我嘗試像這樣(Swift 4)從主 ViewController 推送視圖 controller:

@objc func childAction(sender: UIButton!) {
    print("Child button tapped")
    let vc = childDetailViewController()
    self.navigationController?.pushViewController(vc, animated: true)
}

文本被打印,但 viewController 沒有被推送。 我錯過了什么?

self.navigationController可能為nil

嘗試呈現它: self.present(vc, animated: true)

UPD

另外,如果這樣做沒有幫助,請嘗試將childDetailViewController臨時更改為UIViewController()然后看看會發生什么。 如果點擊后看到空白屏幕,則問題出在childDetailViewController上

AppDelegatedidFinishLaunchingWithOptions內部

let fir = FirstVC()
self.window?.rootViewController = UINavigationController(rootViewController: fir)

那這個

self.navigationController?.pushViewController(vc, animated: true)

應該管用

從 Xcode 11 開始,您可能已經注意到,除了上述默認文件之外,還會創建一個名為 SceneDelegate.swift 的新文件。 從 iOS 13 及更高版本開始,SceneDelegate 承擔了 AppDelegate 的一些職責。 特別是與 AppDelegate 中的 UIWindow 相關的是現在 SceneDelegate 中的 UIScene。 一個應用程序可以有多個場景,主要處理應用程序界面和應用程序內容。 因此,SceneDelegate 負責以 UI 和數據的形式在屏幕上顯示的內容。

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: windowScene)
    let vc = ViewController()
    let navigationView = UINavigationController(rootViewController: vc)
    window?.rootViewController = navigationView
    window?.makeKeyAndVisible()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM