简体   繁体   English

推送没有 storyboard 的 ViewController

[英]push ViewController without storyboard

I try to push a view controller from the main ViewController like this (Swift 4):我尝试像这样(Swift 4)从主 ViewController 推送视图 controller:

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

The text is printed, but the viewController is not pushed.文本被打印,但 viewController 没有被推送。 What did I missed?我错过了什么?

Possible that self.navigationController is nil . self.navigationController可能为nil

Try to present it: self.present(vc, animated: true) 尝试呈现它: self.present(vc, animated: true)

UPD UPD

Also if it does not help, try temporary change your childDetailViewController to UIViewController() and see what happens. 另外,如果这样做没有帮助,请尝试将childDetailViewController临时更改为UIViewController()然后看看会发生什么。 If you see the empty white screen after tap then the problem is on childDetailViewController 如果点击后看到空白屏幕,则问题出在childDetailViewController上

Inside AppDelegate 's didFinishLaunchingWithOptions do AppDelegatedidFinishLaunchingWithOptions内部

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

Then this 那这个

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

should work 应该管用

From Xcode 11 you might have noticed that along with the default files like above, a new file is created named as SceneDelegate.swift.从 Xcode 11 开始,您可能已经注意到,除了上述默认文件之外,还会创建一个名为 SceneDelegate.swift 的新文件。 From iOS 13 and later, SceneDelegate takes up some responsibilites from AppDelegate.从 iOS 13 及更高版本开始,SceneDelegate 承担了 AppDelegate 的一些职责。 In particular related to UIWindow from AppDelegate is now UIScene in SceneDelegate.特别是与 AppDelegate 中的 UIWindow 相关的是现在 SceneDelegate 中的 UIScene。 An app can have more than one scene which mostly handles application interface and app content.一个应用程序可以有多个场景,主要处理应用程序界面和应用程序内容。 So, the SceneDelegate is responsible for what's displayed on the screen in terma of UI and data.因此,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.

相关问题 目前ViewController Segue Transition没有故事板[Swift] - Present ViewController Segue Transition Without Storyboard [Swift] 如何在没有情节提要的情况下设置ViewController的框架 - How to set the frame of a ViewController without storyboard 带有自定义单元格的 ViewController 内的 UITableView 没有情节提要 - UITableView inside a ViewController with custom cell without storyboard 如何在没有情节提要文件的情况下将另一个viewController推到屏幕上? 我已经尝试了以下代码,但没有任何反应 - How do to I push another viewController to the screen without having storyboard file ? I've tried the following code but nothing happens 如何通过故事板将推送通知中的数据传递给ViewController? - How to pass data to a ViewController from push notification through the storyboard? 如何在情节提要中连接下一个后推动viewController - How to push the viewController after the next one connected in the storyboard Xcode Swift MacOs - 隐藏/显示 windows 没有 Z1BC4A1B743127BBF1ED2E36F8 - Xcode Swift MacOs - Hide/Show windows without a StoryBoard and without ViewController 在没有 StoryBoard 的情况下点击通知时访问特定的 ViewController(以编程方式) - Access specific ViewController when notification is Tapped WITHOUT StoryBoard (programatically) 显示 viewController 并在没有 storyboard 和 segue 编程的情况下传递数据 - Show viewController and pass data without storyboard and segue programming Swift 4.2:如何在 ViewController 中嵌入 UIPageControl(没有 Storyboard)? - Swift 4.2: How to embed UIPageControl in ViewController (without Storyboard)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM