使用 self.navigationController 时出现问题?.pushViewController(newViewController, animated: true)

Problems using self.navigationController?.pushViewController(newViewController, animated: true)

这件事对 ios 开发来说是新的,这是我的第二个应用程序,在开发第一个应用程序时,我主要使用了一个主题,然后对其进行了修改,但这个是我从头开始工作的。

我的主要问题是从一个屏幕转到另一个屏幕

当我使用此代码时它有效

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "servicesScreen") as! ServiceViewController
        nextViewController.modalPresentationStyle = .fullScreen
        self.present(nextViewController, animated:true, completion:nil)
   

但我需要使用这个,这样我也可以回去,但这不会做任何事情

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "servicesScreen") as! ServiceViewController
        self.navigationController?.pushViewController(newViewController, animated: true)

我的问题是,我在这里做错了什么还是我需要在 scenedeligate 中添加一些东西?

尝试

select Main.storyboard

select 初始视图控制器

select 编辑器 -> 嵌入 -> 导航控制器。

为此,您需要将导航控制器添加到初始故事板。

如果您使用的是 navigationController,则需要先在故事板上或以编程方式添加导航控制器。

以编程方式添加导航控制器检查此 link:-

要在故事板上添加导航控制器,您应该遵循此 link:- https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html

如果您已将导航控制器添加到您的应用程序,请确保您的 intialViewController 设置为导航控制器,或者在您 运行 您的应用程序时将导航控制器添加到堆栈中。

还要检查“标识符是否正确?您是否引用了正确的情节提要?”。