为什么我在导航时遇到异常。我是iOS开发的初学者。用户登录后我正在导航。我使用了以下代码。我不明白我错过了什么。
我在最后一行收到以下异常 -
Fatal error: Unexpectedly found nil while unwrapping an Optional value
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyBoard.instantiateViewController(withIdentifier: "taskListController") as! TaskListController
self.navigationController!.pushViewController(secondViewController, animated: true)
最佳答案
在推送 VC 之前,必须先添加导航 Controller
使用代码的导航 Controller
func goToMyVC(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let mainView = storyboard.instantiateViewController(withIdentifier: "yourVC") as? yourVC
{
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
nav1.isNavigationBarHidden = true //Show or hide nav bar
nav1.viewControllers = [mainView]
self.window!.switchRootViewController(nav1)
self.window?.makeKeyAndVisible()
} }
在你的 View Controller 中调用这个函数,
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.goToMyVC()
故事中的导航 Controller
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "controller") as? controller
self.navigationController?.pushViewController(vc!, animated: true)
关于ios - 推送导航 Controller 时出现异常 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57218563/