[英]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 在
AppDelegate
的didFinishLaunchingWithOptions
内部
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.