为什么导航时出现异常。我是iOS开发的初学者。一旦用户登录,我便在浏览。我使用了以下代码。我不明白我在想什么。
我在最后一行得到了以下证据-
致命错误:解开可选值时意外发现nil
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyBoard.instantiateViewController(withIdentifier: "taskListController") as! TaskListController
self.navigationController!.pushViewController(secondViewController, animated: true)
答案 0 :(得分:0)
在按下VC之前必须先添加导航控制器
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()
} }
在您的视图控制器中调用此函数,
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.goToMyVC()
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "controller") as? controller
self.navigationController?.pushViewController(vc!, animated: true)
答案 1 :(得分:0)
尝试打击代码,
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "taskListController") as? TaskListController
self.navigationController?.pushViewController(secondViewController!, animated: true)
并确保将登录视图控制器嵌入导航控制器